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

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]

... 16  17  18  19  20  21  22  23  24  [25]  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
5290베도빈2/15/20207213직접 제작한 사용자 정의 콘트롤 DLL, 실행파일에 포함시켜 배포할 수 있을까요? [1]
5288김성배2/14/20207639c# 참조추가 기능에서 DirectX 참조가 보이지 않아요.. DircetX 11 은 설치 되어 있습니다. [1]
5287C# 초...2/14/20206646프로그램 실행 질문 드립니다. [4]파일 다운로드1
5286gongs2/3/202014032윈도우 배율 및 레이아웃에 대한 질문입니다. [3]
5285강성욱1/27/20207766작업관리자에 표시되는 스레드 개수와 프로그램 내 스레드 개수 일치 [1]
5284이상준1/15/202013482출력창에 '스레드가 종료되었습니다' 문구 문의 [2]파일 다운로드1
5282봉봉이 1/13/202010230소켓 비동기 ReceiveTimeOut 구현 [9]파일 다운로드1
5281봉봉이 1/13/20208279소켓 비동기 ReceiveTimeOut 구현 [2]
5280sdd1/8/20206827데이터 저장, 배치파일 관련하여 질문드립니다. [4]
5279김창한12/27/20197815dsoframer ocx [3]
5278guest12/27/20196575CLR 스레드 풀 관련 질문 [1]
5277방문자12/26/20196403WindowsForm 질문드립니다. [3]
5276진우12/17/20198156c++/c# 에도 파이썬 가상환경 같은 기능 혹시 있는지 궁금합니다. [3]
5275송재필12/17/20197882아래 'c#8.0 프로그래밍 교제 예제 관련 문의 드립니다' 의 원인이 압축 파일의 문제인듯 합니다. [2]파일 다운로드1
5272정영훈12/13/20198034byte => string => byte 변환 문의드립니다. [5]파일 다운로드1
5270김세미12/3/20197786c#8.0 프로그래밍 교제 예제 관련 문의 드립니다. [4]
5269달려라라...12/2/20197914C# winform(framework) tabcontrol 에서 button 함수 관련 질문 [3]파일 다운로드1
5266엔벌잉11/26/20196862라즈베리파이 autostart관련 질문 [2]
5265해나11/25/20196946C# 6.0 책 내용 문의 [1]
5264김혜원11/18/20199403C# CodeSign에 대해 여쭤볼 것이 있습니다 [4]
5263정환맨11/17/20197549Task관련 질문입니다 [3]
5262닷린이11/14/20198952Dispatcher 와 synchronizationcontext의 관계가 궁금합니다. [6]
5260방문자11/13/20197898안녕하세요 도움받고 싶습니다 [1]
5259ming11/13/20196983string타입 파라메터로 넘길때 메모리 내부가 궁금합니다. [3]
5258C#초보11/13/20198766DCOM COM+ 등록 관련 문의드립니다. [7]
5257C#초보11/9/20197394파일 저장 방법에 대해 조언 부탁드립니다. [3]
... 16  17  18  19  20  21  22  23  24  [25]  26  27  28  29  30  ...