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

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]

1  2  3  4  5  6  7  8  9  10  11  12  13  [14]  15  ...
NoWriterDateCnt.TitleFile(s)
5590kss12/23/20214563포인터 메모리 에러 [1]
5589초급12/22/20214760c# -> 라즈베리파이(db 접속)시 에러 발생 [7]
5588김지신12/21/20214712안녕하세요 String 변수 참조 주소 질문입니다. [2]
5587이완호12/17/20215814C# SharpDX 화면 캡쳐 관련해서 질문 드립니다. [1]
5586조미김12/16/20214987윈폼에서 메인 스레드와 UI 스레드의 차이점 질문드립니다 [4]
5585김준희12/16/20215749C# 윈폼 TCP/IP 데이터 연속으로 보낼때 [1]
5583난인간이다12/10/20214780.net 6 dynamic pgo 활성화 및 예상되는 문제 [1]
5582김준희12/9/202112019C# TCP/IP 통신시 연결 끊김 에러 [1]
5581김시준12/9/20215329닷넷에서 파일 delete 함수는 왜 비동기가 없는 것인가요? [2]
5580카짜프로...12/7/20216551패턴매칭 -튜퓰비교에 관한 오류사항과 궁금증 [1]파일 다운로드1
5579카짜프로...12/6/2021582911.12 메서드 중복정의에 대한 질문 [1]
5577감사합니...11/30/20214859visual studio 2015 update 3를 다운받을려고 하는데 x64/x86 차이점이 뭘까요? [2]
5576노홍구11/29/20214729C# 으로 USB 스캐너 프린터 리셋하기 입니다. [1]
5575베라11/23/20214804event handler 관련 문의 [2]
5574박원웅11/22/20215109닷넷 프레임워크 산출물의 배포시 해당 환경에 프레임워크 버전이 설치되어 있지 않는 경우를 고려한 배포방법은? [3]
5573mijin11/21/20214937System.NullReferenceException 에 대한 질문 [1]
5572김현진11/21/20215104C# list.Clear() 호출에 대한 문의 드립니다. [4]
5571한예지 donator11/18/20215048무설치 프로그램 원리가 궁금합니다. [2]
5570초보11/16/20215559주식데이터 초당 수신 건수를 구하고 있는데 처리속도가 느려서요 [2]
5569카짜프로...11/14/20216428381페이지 UTC에대한 언급이 그리니치 천문대 시간으로 되어있는게 맞나요? [1]
5568카짜프로...11/14/20216491그림 5.20, 그림 5.22 언급 오류 [1]
5567Edun11/2/20215413쿼리문을 코드로 어떻게 처리할 수 있을까요? [2]
5566민성10/26/20215240Linq에 관해서 [1]
5565pass...10/25/20216493wpf에서 Cefsharp를 AnyCPU로 작업 했는데 실행이 되지 않습니다. [4]
5564초보10/21/20214736오버라이드 관련 질문드립니다 [2]
5562서지훈10/20/20214742win32 api 문의 드립니다. (EnableMenuItem) [5]
1  2  3  4  5  6  7  8  9  10  11  12  13  [14]  15  ...