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

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)
985이성환7/27/201118776P/Inovke 관련 질문입니다. [4]파일 다운로드1
983이성환7/13/201127718Mouse이벤트 관련해서 질문있습니다. [10]파일 다운로드2
982궁굼이7/12/201119169TFS2010 사용관련 [2]
981김창욱7/11/201121708주식형태의 프로그램 처럼 SQL서버의 특정 필드 데이터의 변화가 있을때 재 클라이언트가 정보를 갱신 할 수 있게 하는 방법은 없을까요? [2]
980YJ7/7/201121443App Pool idle time 과 WCF 서비스의 비동기 function 호출. [3]
979왕초보7/1/201119725Apache + IIS Redirect [2]
9776/16/201123461웹컨트롤 어느거 쓰는게 좋나요 [1]
976박성준6/13/201119003VS2008 Add-in 구현 관련 질문 [4]
972김길6/6/201121274메모리 해제 예외 처리 관련.. [2]
971강동원5/29/201118807firebird install건 [1]
970임동찬5/18/201119597ASP.net 솔루션 디버깅 관련 [1]
969이성환5/4/201121230WMI 를 사용하지 않고 하드웨어 정보를 가져올 수 없을까요? [3]
968김동미4/28/201120648안녕하세요 다시 한번 문의를 드립니다.. [2]파일 다운로드1
967임동찬4/22/201123385C# using문 관련 [9]
964김동미4/18/201121010wcf IsOneWay 속성관련 문의 입니다..
965정성태4/18/201122339    답변글 [답변]: wcf IsOneWay 속성관련 문의 입니다..
966김동미4/19/201119802        답변글 [답변]: [답변]: wcf IsOneWay 속성관련 문의 입니다.. [1]
963최재훈4/12/201118634wcf inactivityTimeout 설정시 문의 사항이 있습니다. [2]
962임동찬4/8/201118387TFS 사용관련 [1]
961임동찬4/7/201118529XSD & XML & XmlCodeGenerator [2]
960임동찬4/5/201120270XML Schema Editor [4]
959immm3/24/201118033로그인 연동 어려운 건가요? [1]
958꼭지3/3/201120171Supporting compressed request in WCF 3.5 [5]
957임동찬2/21/201120556WCF channel faulted 관련 [2]
956윤용한2/18/201124071WaitHandle.WaitOne 과 Stopwatch에 관한 질문 [3]
955최광욱2/17/201120751TFS 에서 소스 영구 제거 방법 [1]
... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...