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

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)
5350홍성호6/29/202015775C++에서 정의된 구조체(공유메모리)를 C#으로 공유메모리에서 구조체 변환시 에러가 발생 합니다. [1]파일 다운로드1
5349jero...6/24/202012678logon credential provider 관련 문의드립니다. [1]
5348776/19/202012609시작하세요 C# 8.0프로그래밍 책에 대한 질문입니다. [8]
5347민성6/19/202013184안녕하세요 WPF Binding에서요 [1]
5346dimo...6/18/202013639Task.WhenAll() 메소드를 이용한 코드를 좀더 짧게 사용하고 싶은데 다른 표현이 있을까요? [3]
5345질문요6/12/202012337idc vs 집 속도 [2]
5344하태6/2/202013868c++ .ilb에서 c# dll 호출 질문 드리겠습니다 (콜백함수 전달) [4]
5343하태6/2/202017307안녕하십니까! c# dll을 c++ .lib에서 호출 질문 드립니다. [6]
5342진우5/30/202018038c++ 에서 C# DLL 사용 문의 [2]
5341미나리5/28/202013816스레드 lock키워드 관련 질문드립니다 [3]
5339민성5/27/202012977WPF cmd을 실행을 할때 파라미터 넘기는 방법 [1]
5338서영준5/26/202014897.Net Core Blazor 서버에 Xing API를 이용한 통신 요청 [5]파일 다운로드1
5337ogos...5/26/202015101C# DB connection string 보호 방법에 대하여 [2]
5336saki5/21/202014892이벤트 뷰어 .NET Runtime 오류 [3]
5335민성5/21/202012899안녕하세요 C#으로 컴퓨터 시작프로그램 목록을 가져와서 사용안함으로 바꿀려면 [1]
5334민성5/19/202013418안녕하세요 WPF 콘솔창을 띠우면서 Ping이라는 명령어가 콘솔에 Write되게 할려면 어떻게 해야 하나요?? [1]
5333초보5/18/202013184공유 메모리 관련 문의 [2]
5332질문요5/15/202013361TcpListener TcpClient 문의 [4]
5331sdd5/13/202013065안녕하세요 Settings관련하여 질문드립니다. [2]파일 다운로드1
5330하태5/7/202018115안녕하세요! 질문 드리겠습니다! C# dll에서 c++ CLR프로젝트를 참조 추가 할 수 있나요? [2]
5329김태령4/21/202013014젠킨스에서 원격 머신에 있는 실행 파일을 실행하면 백그라운드로 뜹니다 [2]
5328crea...4/17/202013200안녕하세요! [3]파일 다운로드1
5327김동욱4/17/202013707HttpListener 사용시 HTTP/2 질문입니다. [2]
5323나그네4/15/202012910해결 115p 네임스페이스의 ConsoleApp1.exe는 netcoreapp3.1 폴더에 있었습니다. [1]파일 다운로드1
5322나그네4/14/202013645질문 115p 네임스페이스의 ConsoleApp1.exe 컴파일 또는 빌드방법 [2]파일 다운로드1
5321나그네4/14/202015555오타인가요? [2]
... 16  17  18  19  20  21  22  23  [24]  25  26  27  28  29  30  ...