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

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)
51837/2/20197938.NET Compact Freamwork 컨트롤러 더블버퍼링 [1]
5182wpf ...7/2/20198107wpf 질문 드립니다. [1]파일 다운로드1
51817/1/20198553DataGridview Doublebuffer 에 대해서 질문드립니다. [2]
5180김대훈6/25/20198583배열과 반복문에서 질문드립니다 [2]
5177농상6/13/20199725멀티스레드 건의 [2]
5176이선호6/13/20199316안녕하세요. 닷넷 문제로 검색하다 알게되어 들어왔습니다. 현재 IIS 문제가 있어 질문드립니다. [1]
5175Chobo6/12/20199294WPF Ellipse 그리기! [3]
5174농상6/11/20198610ThreadPool 조인에 관해 [1]
5173전우치6/9/20199057공유 리소스에 대한 스레드 동기화 처리를 위해서 lock 이용 시 문의 [3]
5172김대훈6/7/20197862너무힘드네요 공부에 대한조언부탁드립니다 [2]
5171조남석6/4/20197574EX)11-2(treeview)에 대한 질문입니다. [3]
5170레리6/4/20198174Setup 프로젝트 레지스트리 설정 관련 질문입니다. [1]파일 다운로드1
5169농상6/3/20197957멀티스레드 파라미터 관련 [2]
5168익명유저5/30/20197238항상 정말 감사드립니다... [1]
5167WPF5/23/20198126질문드립니다. [1]
5165이대희5/22/20197285Visual Studio 설치 구성요소 문의 (C# 7.3 개정판 관련) [1]
5164레드5/21/20198802실행 과정과 실행파일 디버그 시 Icon변경 질문드립니다. [5]
5163이대희5/20/20197102시작하세요 C# 7.3 프로그래밍 책 도착했습니다. [1]
5162채홍윤5/14/20199917Mono Develop window 설치 [6]
5161정대영5/13/20197847VS 2013에서 C#6.0(.netFramwork 4.6.1) $ 디버깅 오류 [1]
5160초보개발자5/13/20198839wcf 관련 국내 서적 살만한 책이 있나요? [2]
5159sdh25/9/20198401VS 2010 버전에서 생성한 DLL을 VS 2017버전에서 실행 시 에러 [2]
5158sdh5/8/20197303Visual studio 2010 버전에서 생성한 Project를 Visual studio 2017버전에서 실행에러 [1]
5157김경훈5/8/201910281Task 만들때 넘겨주는 CancellationToken은 어디서 사용 되는 건가요? [4]
5156rysoo5/8/20198528yield return의 리턴 타입 질문 드립니다. [4]
5155세퉁5/7/201910457관리자 권한으로 실행 시 알림창(?) 없이 바로 실행 시킬 수 있는 방법이 있을까요?? [2]파일 다운로드1
... 16  17  18  19  20  21  22  23  24  25  26  27  [28]  29  30  ...