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

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)
1053폭풍코딩4/19/201216664스마트클라이언트 ctrl-c 복사가 작동하지 않습니다. [1]
1052에슈리온4/17/201233110ClickOnce 배포시 클라이언트의 설치경로를 지정할 수 없나요? [1]
1051최정수4/10/201220002WCF 클라이언트 비정상 종료 관련 [1]
1050에슈리온4/6/201222162관리자 권한과 ClickOnce, 그리고 Bootstrapper문제 [6]
1049김성혁4/4/201216747스마트클라이언트 stand-alone 방식의 배포 문제.. [1]
1048C#조으다3/27/201220276어셈블리 로드 / 언로드와 관련해서.. [6]
1047김우형3/23/201217887WCF 서비스를 이용한 데이터 전송 중 Exception 문의 [1]
1044임동찬2/29/201217744웹에서 COM Exception.. [2]
1043박성준2/24/201216282Lazy<T> 의 지연객체 생성 전에 실 객체의 Attribute분석 [2]파일 다운로드1
1042이성환2/20/201216708BlockingMethod에 빠진 스레드를 즉시 죽이고 싶습니다...;ㅅ; [2]파일 다운로드1
1039김재영2/15/201216049어셈블리에 사용자마다 다른 값을 적용하여 자동 빌드 방법이 있을까요? [2]
1037윈드로니2/12/201217485WPF 관련 질문 드립니다. [2]
1035임동찬2/9/201215853XML, XSD, XMLCodeGenerator 관련 [2]
1033곽성현2/7/201216375훔..윈도우 디바이스 드라이버 관련 [1]
1030궁빈2/2/201218806msbuild를 이용한 웹게시 및 자동 Dist 방법론에 대해 질문 드립니다!! [6]
1028신정환1/30/201216006기존 legacy Windows application이 WoA 환경에서 동작되지 않는 이유가 무얼까요? [2]
1026노현철1/19/201217967WPF가 XP에서 느리게 로딩되는 현상에 대해 질문드립니다. [4]
1025궁그미1/18/201218929네이버 소켓 로그인 이해가 안되는점이 있는데요 [2]
1022선무당1/11/201216965TFS 에서 공통 Assembly 공유 방법에 대해서 해결책이 없을까요? [2]
1021김재영1/8/201216103대리자를 메소드 파라메터로 넘겨도 됩니까? [2]파일 다운로드1
1019남산골11/13/201918713안녕하세요!! 성태님~~ [3]
1018강한구12/29/201121993WPF ClickOnce 배포시 콤포넌트 dll 다운로드 문제에 대하여 질문 있습니다. [1]
1017임동찬12/28/201118932smart client와 xbop, silverlight [6]
1015개똥이12/22/201116347RMCLOCK 설정 관련 질문이요ㅜ [2]
1011사길수12/16/201116399hyper-v 이더넷 카드 관련 문의 좀 드립니다. [1]
1006루로니12/13/201118497윈도우7과 XP의 DLL후킹 처리에 관해서 질문입니다. [3]
... 61  62  [63]  64  65  66  67  68  69  70  71  72  73  74  75  ...