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

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)
4749강준8/11/201612160ElementHost Memory Leak 현상 [6]
4748Bere...8/3/201611011그냥 생각이 들어서 여기 글 써봅니다. [1]
4746힘찬도약8/2/201611271[asp.net] local에서 cookies값이 읽혀지지 않는 경우 [1]
4747힘찬도약8/3/201612051    답변글 [예제 첨부]: [asp.net] local에서 cookies값이 읽혀지지 않는 경우 [3]파일 다운로드1
4745힘찬도약7/27/201612139.NET 자식창 데이터를 부모창에 전달시 오류 질문드립니다. [4]파일 다운로드1
4744변찬연7/27/201611953안녕하세요 그 edge를 이용하는 데 조금 불편함이 있어서 문의드립니다 [1]
4743딸랑구아빠7/26/201610833IE 사용 시 인증 정보 계속 보내기? [1]
4741차가워7/20/201612458UWP 에서 COM 참조 불가능한가요? [6]
4740luna...7/19/201612902clickonce manifest 파일 질문 [1]
4739윤똘씨7/19/201612386정말 감동적입니다... [7]
4738닷넷초보7/7/201611215안녕하세요. C#의 개체 전달방식(참조)에 관련해서 질문 있습니다. [3]파일 다운로드1
4737beau...7/6/201613542python embedding 한 c++ 프로그램의 배포에 관해서 질문드리고 싶습니다. [1]
4736spow...7/6/201610423Property Lambda에 대한 심층 분석을 의뢰합니다 [7]
4735힘찬도약7/5/201611577.Net/Web.config에서 MSSQL을 쓰는데, MysqlSiteMap 참조오류 [3]파일 다운로드1
4734spow...6/28/201611160Visual Studio 2015 + Windows Forms 환경에서 컴파일 할 때 드물게 개발환경이 Crash 됩니다 [1]
4733feel...6/15/201611631타 서버로 소스 이전 중 [3]파일 다운로드1
4732구봉근6/14/201610387안녕하세요 [1]파일 다운로드1
4731초급개발자6/7/201612868Windows Form Application는 사라질까요? [2]
4730김시현6/4/201610943책으로 공부하던 도중 생성자에 대해서 궁금한게 생겼는데 혹시 답변좀 해주실수 있나요? [1]
4729Jeah...6/3/201612368C#.Net 프로그램에서 C++로 만들어진 dll 파일 이용관련 문의 하나 드립니다. [3]
4727링거5/30/201611018WPF MediaElement 파일 실행 후 삭제시 오류 문의 [1]파일 다운로드1
4728링거5/30/201611388    답변글 [답변]: WPF MediaElement 파일 실행 후 삭제시 오류 문의 [2]파일 다운로드1
4726spow...5/25/201610817덧글을 달고 나서 F5를 눌렀을 때 똑같은 덧글이 달리는 현상 고쳐주세요 [4]
4725Futu...5/23/201611295책 잘보고 있습니다. Task관련 질문을!!! [1]
4724학생15/23/201612554웹소켓과 소켓 관련해서 질문 드립니다. [2]
4722지나가던...5/15/201614389Part 3 pdf파일 그림 재수정 문의 [1]
... 31  32  33  34  35  36  37  38  39  40  41  42  43  [44]  45  ...