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

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)
5619팡팡이3/3/20225791RSA 문의드립니다. [3]
5618김기헌3/2/20224334안녕하세요 생성자 옆에 this 키워드를 붙여 생성자를 여러 개 호출 시 질문드립니다 [2]
5617Edun2/25/20224460ArgumentOutOrRangeException에러 발생 [2]파일 다운로드1
5616csha...2/24/20224358readonly struct로 선언된 구조체를 특정 클래스에서 멤버변수로 가지는 경우 [1]
5615장성욱2/22/20226548SetThreadAffinityMask를 이용한 쓰레드 지정하는 방법에 대해 궁금합니다. [4]
5614민우2/22/20225834SSL 통신 문의 [6]
5613김인태2/22/20224388서버와 PC 간의 어플리케이션 성능 차이 [1]
5612팬입니다2/20/20224191Kastrel 서버 관련 [1]
5611차가워2/19/20224428stopWatch 늘어짐 문의 [3]
5610차가워2/18/20224318Stopwatch 늘어짐 문제 [1]
5609cs린이2/15/20224653c# 함수의 호출 방식에 대해 궁금합니다! [2]
5608지호2/10/20225161시작하세요 C# 8.0 중 제네릭타입의 IEnumerable [3]파일 다운로드1
5607이로운2/10/20224403안녕하세요. 궁금한게 있어서 질문드립니다. [1]
5605강성봉2/10/20224426TCP PSH flag 패킷 수신 에러 [1]
5604LW2/9/20224565VISUAL STUDIO 2019 ==> 2020 설치시 오류가 생겨서 문의드립니다. [3]
5603김진명2/9/20225839C# 10.0 출간은 언제쯤 계획하고 계신가요? [1]
5602신갑영2/8/20224642윈폼에 대해서 질문 드립니다. [1]
5601김인태2/4/20224883setup project 관련 [7]
5600itkim2/3/20225693윈도우 서버 계정 패스워드 인증 문의 [5]
5599레드골드2/3/20226167c#으로 ms word 제어 가능할까요? [6]
5598jaew...2/2/20224862Dictionary는 참조형식인가요?? [1]
5597재원2/2/20225206c# 9.0에 대한 내용을 받을 수 있나요? [1]
5596kss1/26/20224933듀얼 모니터 환경에서 wpf 프로그램 크기 변경 [2]
5594mira...1/25/20224580안녕하세요 try~catch 와 SuspendLayout~ResumeLayout 간 호출 문의드립니다! [2]
5593C#스터디1/12/20225483TaskAwaiter 구현 질문 입니다. [1]
5591유지킴12/24/20215110outofmemory in 32bit [2]파일 다운로드1
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...