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

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)
1093이지수10/6/201219296vbs [1]
1092김성수9/20/201220559안녕하세요? MS Windows server 2008 R2 에서 IIS7 의 SSL 설정시에 에러 해결좀 도와주세요 [1]
1089한상욱9/13/201220420azure에 관한 질문입니다. [1]
1088김영대9/12/201223084DirectX.AudioVideoPlayBack 참조하여 wav 재생기를 제작하여 SmartClient 를 이용하여 aspx 페이지에 Embed 하는 과정의 질문 사항입니다. [2]
1087한우주9/10/201223702SSL 모든 웹페이지에 적용해야하나요? [2]
1086한우주9/4/201221389SSL 인증서 범용으로 사용하기 [3]
1085한우주9/4/201219223개발환경구성 18.3.1 에 대한 문의 [2]파일 다운로드1
1084김종하9/3/201220226team foundation express 2012에서 새 프로젝트 생성... [3]
1082김대경8/23/201219090windows2008 에서 ActiveX 사용 시 문제점. [3]
1091김대경9/20/201219140    답변글 [답변]: windows2008 에서 ActiveX 사용 시 문제점. [2]파일 다운로드1
1081Seon...8/20/201221131WCF 파일 업,다운로드 문제 [2]
1080배동선8/13/201220292정품 라이센스 인증 개발에 대해 조언을 구하고 싶습니다 [4]
1079송준호8/11/201218085basicHttpBinding stream 전송에 관하여 질문드립니다. [1]
1078Ho S...8/10/201217227윈도우폰 지도 구현에서 ㅠㅠㅠ 질문드립니다. [1]파일 다운로드1
1077한우주8/6/201219909웹서비스 호출 후 세션 유지하기 [1]
1076이성환8/2/201220513dynamic 변수 할당은 도대체 어디에???? [1]
1073pass...7/24/201219898c# 으로 만드는 음성인식/TTS 프로그램에 관련해서.. [3]
1072일반인7/23/201222579rss 만드는 방법 알려주실 수 있으신가요? [5]
1069임경훈7/20/201223934아래와 같은 오류후 IIS가 다운되는 현상이 발생합니다. [1]
1068신호철6/15/201222112ClassLibrary COM+ 등록 안되는 현상 [2]
1067김은주6/14/201232354소스세이프 사용시 pfx 파일 빌드 실패 [3]
1066너무초보6/3/201218726안녕하세요! 디렉토리 삭제 오류 질문 좀 드립니다. [4]
1065이성환5/23/201220704Assembly.LoadFrom Assembly.Load 의 차이점이 궁금합니다. [2]파일 다운로드1
1064푸우5/21/201219512GPU 디바이스 재시작시 WPF + .NET 3.5 기반 어플리케이션 UI 문제 [2]
1063발코더5/20/201219349귀신 곡할 상황 질문입니다. [3]
1062김규덕5/12/201219740ffmpeg 관련 조언 구합니다. [2]
... 61  [62]  63  64  65  66  67  68  69  70  71  72  73  74  75  ...