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

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]

... 31  32  33  34  35  36  37  38  39  40  41  42  43  [44]  45  ...
NoWriterDateCnt.TitleFile(s)
4776권오영11/11/201617319제가 이클립스를 공부중인데..이상한것을 찾았습니다.. [2]
4775이성환11/11/201621118안녕하세요. SnapsToDevicePixels 질문입니다. [5]파일 다운로드1
4774popo11/10/201617627.net SSL통신 관련 질문 드립니다. [1]
4773김상호11/4/201619884재귀함수 반복문 변환 [1]파일 다운로드1
4772자연인10/27/201620786hwpctrl을 사용하는 사이트에서 나와 브라우저를 종료하면 오류메세지가 나옵니다. [1]파일 다운로드1
4771문종훈10/18/201620881.net 소스 질문이 있습니다 [2]
4770누구게~...10/15/201617869세도나 [1]
4769spow...10/13/201616769올리시는 게시물에 '좋아요'를 선택할 수 있도록 해주세요 [3]
4768브라운10/11/201618525질문 하나만 드려도 될까요 [4]
4767암호군10/4/201623577c# aes 128 암복호화 관련 문의드립니다. [3]
4766김신철9/29/201618232Visual Studio 2015에서 .net 3.5로 c# 6.0 사용시 문제점에 대해서 궁금합니다. [1]
4765spow...9/23/201617392참조를 통해 속성의 값을 변경하고 싶을 때 우아한 코딩 방법이 있을까요? [2]
4764지현명9/22/201619051Visual Studio 2008 c#에서 추가된 솔류션의 디버깅이 안걸립니다. [2]파일 다운로드1
4763송기태9/20/201617555안녕하세요! 질문이 있어 문의드립니다! [1]파일 다운로드1
4762김신철9/20/201618821Visual Studio 2015 마이그레이션 후 빌드 및 에러 문제.. 도와주세요~ [2]
4761JH9/19/201619319WPF로 Viewbox 사용 시 폰트 크기 일정화 여부 [1]
4760초보9/18/201619645유닉스서버(HP)에서 C# 서버 프로그램 실행 가능 한지요? [1]
4759dev009/16/201621090Queue out of memory [3]
4758임기성9/12/201620120MS오피스 워드 64비트에서 32비트 COM개체 사용방법 문의 [2]
4757조영준9/7/201617692DLL 후킹과 관련해서 질문이 있습니다. [2]
4756Kim ...9/6/201619900drag&drop 관련해서 문의 드립니다. [6]
4755stel...9/4/201618528안녕하세요! 윈도우 창에 관련되서 질문입니다.! [3]
4754초보개발자8/25/201617369UWP 의 적용 범위에 대해서 어떻게 생각하십니까? [1]
4753조호찬8/23/201623067sybase 의 한글 가져오기 문의 [7]
4752타미플루8/19/201617904IIS 로그에서 time-taken이 0이 나올수 있나요? [4]
4751김민석8/16/201618037가변크기의 구조체를 SendMessage로 타 프로세스에 전송하는 방법이 있을까요? [1]
... 31  32  33  34  35  36  37  38  39  40  41  42  43  [44]  45  ...