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

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)
5487진우4/27/202113377C# 엑셀 자동화 성능 향상 문의 [2]
5486지나가던...4/26/202113170닷넷 구현 코드 관련 질문 [2]
5485이재원4/17/202113349교재 315페이지 내용 질문 [3]
5484Syong4/16/202117301윈폼 기반의 응용프로그램 dll 참조와 32,64bit 빌드 관련 문의 [4]
5483한예지 donator4/15/202112334익명 형식과 var 관계 질문 있습니다. [2]
5482질문4/13/202114286WPF를 위한 MVVM toolkit 선택과 관련한 문의드립니다. [4]
5480한예지 donator4/5/202115187GetHashCode 질문있습니다! [2]
5479한예지 donator4/4/202114054Equals를 닷넷에서 어떻게 구현했는지 보고 싶을 떄는 어떻게 해야 될까요? [2]
5478갑자기C#3/23/202117035C# Winform에서 TextBox없이 입력 받을 수 있나요? [7]
5477달팽이3/18/202113192c# 도형을 그린후 편집하는 방법이 궁금합니다. [2]파일 다운로드1
5475dimo...3/2/202114093POH가 .NET 5에 추가된 것으로 알고 있습니다. POH가 유용한 경우는 어떤 경우가 있을까요? [1]
5474Syong2/26/202113622사용자 지정 컨트롤 생성시 Invalidate, Update, Refresh의 차이점 [2]
5473한예지 donator2/25/202113059디자인 타임이 뭔지 궁금합니다!! [5]
5472dimo...2/24/202114501등록하지 않고 ocx를 사용하는 `키움`관련 포스팅을 따라가다 질문드립니다. [9]
5471남산2/21/202112956MarshalDirectiveException 에 대한 질문입니다. [1]
5470한예지 donator2/7/202112686yield 질문있습니다!! [2]
5469유호성2/6/202112542Parallel + Task.Run 동시 실행 환경에서 간헐적으로 Task.Run()에서 null이 리턴됩니다. [3]파일 다운로드1
5467hero...2/4/202113111실행 환경에 따른 Thread.Sleep 딜레이 차이 질문 [8]
5466pr1/29/202112962c# winform load시 작업표시줄에 뜨지 않는 현상을 겪으신적이 있으신가요? [4]파일 다운로드1
5465영귤1/28/202113127두 번째 await 부터는 스레드 개수만 늘어나는 것이 아닌가요 [1]
5464민우1/26/202113944C# 빌드시 코드 치환되는걸 확인하는 방법 문의 [2]
5463한예지 donator1/24/202114677Parameters.AddWithValue 와 Parameters.Add 의 차이점이 궁금합니다. [2]
5462C#초보1/22/202114807사용자 정의 메시지 전달이 가능한가요? [2]
5461한예지 donator1/22/202113840AsEnumerable() 메서드 질문 있습니다! [2]
5459한예지 donator1/21/202113132typeof와 GetType의 차이점 질문있습니다. [2]
5458진우1/20/202114333C# DataTable 에 SQL 쿼리문을 실행하는 방법 문의 (LINQ 사용하지 않고) [6]
... 16  17  18  [19]  20  21  22  23  24  25  26  27  28  29  30  ...