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

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)
4774popo11/10/201610968.net SSL통신 관련 질문 드립니다. [1]
4773김상호11/4/201613459재귀함수 반복문 변환 [1]파일 다운로드1
4772자연인10/27/201614466hwpctrl을 사용하는 사이트에서 나와 브라우저를 종료하면 오류메세지가 나옵니다. [1]파일 다운로드1
4771문종훈10/18/201614494.net 소스 질문이 있습니다 [2]
4770누구게~...10/15/201611714세도나 [1]
4769spow...10/13/201610555올리시는 게시물에 '좋아요'를 선택할 수 있도록 해주세요 [3]
4768브라운10/11/201612281질문 하나만 드려도 될까요 [4]
4767암호군10/4/201616652c# aes 128 암복호화 관련 문의드립니다. [3]
4766김신철9/29/201611510Visual Studio 2015에서 .net 3.5로 c# 6.0 사용시 문제점에 대해서 궁금합니다. [1]
4765spow...9/23/201610916참조를 통해 속성의 값을 변경하고 싶을 때 우아한 코딩 방법이 있을까요? [2]
4764지현명9/22/201612480Visual Studio 2008 c#에서 추가된 솔류션의 디버깅이 안걸립니다. [2]파일 다운로드1
4763송기태9/20/201611236안녕하세요! 질문이 있어 문의드립니다! [1]파일 다운로드1
4762김신철9/20/201612368Visual Studio 2015 마이그레이션 후 빌드 및 에러 문제.. 도와주세요~ [2]
4761JH9/19/201612795WPF로 Viewbox 사용 시 폰트 크기 일정화 여부 [1]
4760초보9/18/201612726유닉스서버(HP)에서 C# 서버 프로그램 실행 가능 한지요? [1]
4759dev009/16/201613532Queue out of memory [3]
4758임기성9/12/201613099MS오피스 워드 64비트에서 32비트 COM개체 사용방법 문의 [2]
4757조영준9/7/201611120DLL 후킹과 관련해서 질문이 있습니다. [2]
4756Kim ...9/6/201613190drag&drop 관련해서 문의 드립니다. [6]
4755stel...9/4/201612027안녕하세요! 윈도우 창에 관련되서 질문입니다.! [3]
4754초보개발자8/25/201610920UWP 의 적용 범위에 대해서 어떻게 생각하십니까? [1]
4753조호찬8/23/201615806sybase 의 한글 가져오기 문의 [7]
4752타미플루8/19/201611398IIS 로그에서 time-taken이 0이 나올수 있나요? [4]
4751김민석8/16/201611592가변크기의 구조체를 SendMessage로 타 프로세스에 전송하는 방법이 있을까요? [1]
4750강준8/13/201613147ElementHost Memory Leak 현상 (아래내용과 동일 첨부 추가^^) [5]파일 다운로드1
4749강준8/11/201612159ElementHost Memory Leak 현상 [6]
... 31  32  33  34  35  36  37  38  39  40  41  42  [43]  44  45  ...