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

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)
5920한예지 donator10/3/202313718C#과 WIN32 API 관계 질문드립니다. [4]
5919이건우9/27/202312382WinForm의 로딩속도 관련 질문입니다 [2]
5917한예지 donator9/14/202313098동기화 도구 질문 있습니다. [4]
5916한예지 donator9/3/202313496Thread.Sleep(500), await Task.Delay(500), Task.Delay(500) 차이점이 궁금합니다. [2]
5915한예지 donator8/30/202314928비동기 코드를 for 문 안에 작성한 경우 제어 변수가 올바르게 동작하지 않는 이유가 궁금합니다. [3]
5914한상욱8/11/202314455.net wpf에서 skiasharp 의 skelement 를 canvas로 사용 하고 있습니다. [1]
5913김태우8/10/202314030지역변수로 이해하는 메서드매개변수 게시글 댓글 [3]
5912guest4/25/202319503[참고 - 초보용] Sqlite 디비는 double이 없고 Real이 대신합니다 [3]
5911guest4/24/202313213Form1.cs와 외부 class.cs와 통신 (static async method포함) [4]파일 다운로드1
5910guest4/24/202312558Async 메서드와 try~catch [1]
5909guest4/22/202314185Visual Studio 구매 시(1인 개발자) [4]
5908guest4/22/202313401텅빈 원그리기 [5]
5907민성4/21/202313405안녕하세요 서버 백업 문제에 대해서 [2]
5906guest4/21/202313673Dispatcher 서비스 구현 질문 [1]
5905guest4/20/202314746tabControl의 tabPage가 여러 개일 때 순서를 바꾸기가 까다롭네요 [5]
5904guest4/18/202315063[신규자료첨부] DLL 'SQLite.Interop.dll'을 찾을 수 없습니다 [4]파일 다운로드1
5903guest4/18/202313680fileSystemWatcher 이벤트 관련 질문입니다 [2]
5902guest4/17/202314827c#으로 USB 관련 질문 [2]
5901guest4/17/202312083내솔루션 판매 시 1.0.0.0 폴더와 Sqlite 배포 [5]
5900guest4/17/202315240DLL 'SQLite.Interop.dll'을 찾을 수 없습니다 [2]파일 다운로드1
5899guest4/17/202313206Dictionary와 Linq [4]
5898차가워4/17/202312796CNTK 교육 문의 [1]
5897guest4/17/202312403Socket스레드와 UI thread [4]
5896HAN4/16/202312494c언어 return 에 대해 궁금한게 있어요. [1]파일 다운로드1
5895guest4/15/202312707Drag and Drop - 모든 컨트롤 [2]
5894송부장4/14/202314666[질문] Visual Studio 2022에서 '도구 상자 항목 선택'의 'COM 구성 요소' 탭에서 ActiveX 목록이 보이지 않습니다. [3]파일 다운로드2
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...