Microsoft MVP성태의 닷넷 이야기
C# 7.1 185~187p 질문입니다. [링크 복사], [링크+제목 복사],
조회: 16796
글쓴 사람
꾸엉
홈페이지
첨부 파일
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Mathematics
{
    delegate int CalcDelegate(int x, int y);

    static int Add(int x, int y) { return x + y; }
    static int Subtract(int x, int y) { return x - y; }
    static int Multiply(int x, int y) { return x * y; }
    static int Divide(int x, int y) { return x / y; }

    CalcDelegate[] methods;

    public Mathematics()
    {
        methods = new CalcDelegate[] { Mathematics.Add,
            Mathematics.Subtract, Mathematics.Multiply, Mathematics.Divide };
    }

    public void Calculate(char opCode, int operand1, int operand2)
    {
        switch (opCode)
        {
            case '+':
                Console.WriteLine("+ : " + methods[0](operand1, operand2));
                break;

            case '-':
                Console.WriteLine("- : " + methods[1](operand1, operand2));
                break;

            case '*':
                Console.WriteLine("* : " + methods[2](operand1, operand2));
                break;

            case '/':
                Console.WriteLine("/ : " + methods[3](operand1, operand2));
                break;
        }
    }
}

namespace Study1
{

    class Program
    {
        delegate void WorkDelegate(char arg1, int arg2, int arg3);

        static void Main(string[] args)
        {
            Mathematics math = new Mathematics();
            WorkDelegate work = math.Calculate;

            work('+', 10, 5);
            work('-', 10, 5);
            work('*', 10, 5);
            work('/', 10, 5);
        }
    }
}



델리게이트를 공부하고 있는데요

왜 이번 예제에서는 Mathematics 클래스의 접근 제한자를 public으로 해서
namespace 밖에서 선언(?)을 한건가요?



그리고 static int Add(int x, int y) { return x + y; }
이 부분에서 static을 지우고 실행하면


        methods = new CalcDelegate[] { Mathematics.Add,
            Mathematics.Subtract, Mathematics.Multiply, Mathematics.Divide };

이 구문에서 Mathematics.Add 부분이 오류가 발생하던데

이 코드에서 static이 어떤 역할을 해주기에 static을 지우면 오류가 발생하는건가요?
ㅜㅜ static에 대한 개념을 잘 모르겠습니다..








[최초 등록일: ]
[최종 수정일: 10/29/2018]


비밀번호

댓글 작성자
 



2018-10-29 10시22분
namespace는 임의 관리 단위일 뿐 프로그램의 논리와는 상관이 없는 부분입니다. 그리고 static을 빼면 instance 멤버가 되기 때문에 그 앞에 클래스 이름을 붙일 수 없습니다. 따라서 "Mathematics.Add"라고 하면 안 되고 그냥 "Add"라고 해야 합니다.


정성태
2018-10-29 01시00분
[꾸엉] 감사합니다~!
[guest]

... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...
NoWriterDateCnt.TitleFile(s)
736김현기1/24/200817344XML 접두어 포함한 XmlElement 작성법? [1]
735정명원1/9/200817193ActiveX 사용시 ... [2]
734제영한12/17/200717220웹서비스에서 사용자 정의 예외 사용하기 [2]
732김기룡12/3/200720336WebRequest 요청시 일본어 인코딩 관련 문의 [3]파일 다운로드1
731우기11/27/200714895[질문] clickonce 배포시 .net framework 2.0, 3.0 설치 시간 줄일 수 있는 묘수없을까요?? [1]
730김재영11/10/200716093스레드에 사용되는 함수에 값을 같이 넘기는 방법이 있습니까?? [2]파일 다운로드1
727제영한10/24/200714877시스템 설계에 대한 문의드립니다. [2]
726한귀순10/24/200716264SQL 2005 Express 사용시.. [3]
725이방은10/23/200716730성태님 GDI+질문있어요.. [2]
724clic...10/17/200717724clickonce 작동시 무반응입니다. [1]
722한귀순10/11/200721319Internet Explorer 7.0 에서 ClickOnce Error [3]
721이카루스10/5/200717691WCF 질문드립니다. [1]
720limj...10/5/200717250인증서/스마트 카드에 기반한 Managed Card - STS 구현 질문 [2]
718DJ.처리10/2/200726431[VS.NET 2008] 스마트클라이언트 게시 오류...도와주세요~ [2]파일 다운로드2
716guest9/28/200720964실행시간이 긴 com+ 객체가 죽는거 같습니다. 어떻게 해야 할지.... [3]
713하민규9/19/200715643VC++ 6과 Vista UAC .. [1]파일 다운로드1
711술푼어둠이9/17/200716211사설 IP 사용 문제?? [1]파일 다운로드1
709한귀순9/14/200715724asp.net 에서의 paging 관련. [2]
708이준영9/13/200717860마우스 커서의 움직이는 속도 알아내는 좋은 방법은? [1]
707hatu...9/10/200716016window2000과 ssl 그리고 frame [2]
701소쿠리9/5/200716647Get 으로 넘긴 한글 문자열 Respose.AddHeader 에서 filename 으로 사용시 깨짐. [1]
699유현기9/3/200716375[질문1]스마트 클라이언트에서 웹의 변수를 스마트 클라이언트에서 받아쓰기(제발도와주세요 ㅠ.ㅠ) [1]파일 다운로드1
696clev...8/31/200715935Exe COM으로 만들면 혹시 Bridge dll없이도 권한 상승이 가능한가요? [2]
695스마일8/30/200715803Visaul Studio 2005에서 디버깅이 안되요...ㅠㅠ [1]
694이호정8/29/200716095[CardSpace 관련 질문] 정리해 좋으신 글 잘 보았습니다. [4]
693장근배8/29/200715766매핑 구간이 열려 있습니다 [3]
... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...