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

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)
888김재영7/5/201014372TFS에 반드시! SQL Report랑 SharePoint(or WSS)가 붙어야 합니까? [2]
887임동찬7/2/201015696WCF sendTimeout에 관하여... [1]
886김재영6/16/201015701스레드 선언시 (Parameterized/)ThreadStart에 정의되는 메소드의 위치에 질문이 있습니다. [3]
885장근배6/13/201023107Win32Exception 창 핸들 에러 [1]
883채동민6/10/201015293비동기 DB 쿼리관련 질문
884정성태6/10/201016746    답변글 [답변]: 비동기 DB 쿼리관련 질문
881최준영5/24/201016521load되지않은 아이템 load하는 방법? [1]
880임상일5/10/201018616VS2010 TestManager를 통한 UI Test 관련 질문입니다. [2]파일 다운로드1
879정용훈5/3/201019856wcf 인증 문제 [2]
878채동민4/20/201019058WCF에서 maxItemsInObjectGraph 오류 관련 질문드립니다. [2]
872날쌘돌이4/8/201021754Windows7 에서 IIS에서 폼인증 으로 디버깅.. [1]파일 다운로드1
871영초4/7/201017117실버라이트로 스캐너 구동 프로그램이 가능할까요? [1]
869김재영4/6/201015998이미 실행된 어셈블리 컨트롤 권한을 다른 어셈블리에서 가져올 수 있습니까? [2]
868Lime3/5/201019601WCF 에서의 DataTable 사용 [2]
867Dani...2/18/201019733Question - HTTP 401.3 on DELETE, PUT verbs [6]파일 다운로드1
865박근대2/16/201016217WCF 오류 문의. [1]
863날쌘돌이1/31/201018563ActiveX Cab에서 닷넷 dll 등록 [1]
862장근배1/28/201016345AppPolId를 알 수 있는 방법이 있는지요? [1]
864장근배2/1/201016005    답변글 [답변]: AppPolId를 알 수 있는 방법이 있는지요?
858생초보1/26/201016363안녕하세요.. 파일 생성 처리 문제로 .... [2]
857꼭지1/22/201022106How to support Basic + Windows authentication mode in WCF(RESTful service) [4]파일 다운로드2
856질의자1/13/201020068vcredist_x86 배포 관련 여쭙습니다. [1]
855임동찬1/13/201018165이벤트 핸들러와 쓰레드의 관계 2 [4]파일 다운로드2
854임동찬1/12/201018449이벤트 핸들러와 쓰레드의 관계 [1]
853장근배1/12/201018047압축 프로그램,, [1]
852임동찬1/6/201015439base.OnStart(agrs) [1]
... 61  62  63  64  65  66  67  [68]  69  70  71  72  73  74  75  ...