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

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)
979왕초보7/1/201117973Apache + IIS Redirect [2]
9776/16/201122277웹컨트롤 어느거 쓰는게 좋나요 [1]
976박성준6/13/201117769VS2008 Add-in 구현 관련 질문 [4]
972김길6/6/201120038메모리 해제 예외 처리 관련.. [2]
971강동원5/29/201117560firebird install건 [1]
970임동찬5/18/201118353ASP.net 솔루션 디버깅 관련 [1]
969이성환5/4/201120025WMI 를 사용하지 않고 하드웨어 정보를 가져올 수 없을까요? [3]
968김동미4/28/201119436안녕하세요 다시 한번 문의를 드립니다.. [2]파일 다운로드1
967임동찬4/22/201122179C# using문 관련 [9]
964김동미4/18/201119779wcf IsOneWay 속성관련 문의 입니다..
965정성태4/18/201121152    답변글 [답변]: wcf IsOneWay 속성관련 문의 입니다..
966김동미4/19/201118980        답변글 [답변]: [답변]: wcf IsOneWay 속성관련 문의 입니다.. [1]
963최재훈4/12/201117363wcf inactivityTimeout 설정시 문의 사항이 있습니다. [2]
962임동찬4/8/201116982TFS 사용관련 [1]
961임동찬4/7/201117337XSD & XML & XmlCodeGenerator [2]
960임동찬4/5/201118866XML Schema Editor [4]
959immm3/24/201116670로그인 연동 어려운 건가요? [1]
958꼭지3/3/201118699Supporting compressed request in WCF 3.5 [5]
957임동찬2/21/201119209WCF channel faulted 관련 [2]
956윤용한2/18/201122649WaitHandle.WaitOne 과 Stopwatch에 관한 질문 [3]
955최광욱2/17/201119313TFS 에서 소스 영구 제거 방법 [1]
954한장우2/16/201117035atl activeX 질문이요~ [1]
952박용운2/16/201117898IE8.0에서 BHO [1]
953박용운2/16/201117897    답변글 [답변]: IE8.0에서 BHO
951임동찬2/11/201117926WCF Service Reference [1]
950이성환2/9/201119384Windows application 프로젝트를 참조 했을 때 생성되는 실행파일을 직접 실행 불가능하도록 하고 싶습니다. [6]파일 다운로드1
... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...