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

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]

... 31  32  [33]  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
5083거북이11/4/201819524타입의 범위를 넘어서는 연산의 개념을 모르겠습니다. [4]
5082꾸엉11/1/201815866BCL 타입을 모아둔 곳이 있나요? [2]
5081꾸엉10/31/201816397C# 7.1 235p 질문입니다. [3]
5080WPF초보10/30/201818666[WPF] IsManipulationEnabled속성 설정시 Click이벤트 문의 [1]
5079jhp10/30/201818897이 소스에 API후킹 소스를 추가하고 싶은데 어떻게 해야될지 모르겠어요. [4]
5078꾸엉10/29/201816852C# 7.1 185~187p 질문입니다. [2]
5077윤현수10/29/201823915setup파일 설치 이후 실행이 안되는 현상 [11]
5074진우10/28/201816727C# 생성과 동시에 초기화 하는 코드 문의 [2]
5073돌고래10/27/201816384c# 공부 방향 질문 드립니다. [3]
5072엔벌잉10/24/201815682C# textbox, button질문입니다! [4]파일 다운로드1
5071엔벌잉10/23/201817381C#윈도우폼 질문입니다!! [2]
5070진우10/17/201817442Visual Studio 서비스팩과 업데이트 차이 문의 [2]
5069감자10/12/201818021빌드 구성을 재설정하는 방법이 있을까요? [1]파일 다운로드1
5068누오10/10/201816815ASP Core 2.0 에서 dll안에 있는 뷰 읽어들이는 방법? [1]
5067김정민10/5/201817436다른 윈도우가 깨지는 현상을 막을 수 있을까요 [3]
5066로니브10/4/201817869ASP.NET MVC에서 View 파일 숨기는법? 보안처리 하는법? 관련 질문.. [1]
5065키모10/1/201819706문자 질문입니다. [3]
5064로니브10/1/201819044클래스 라이브러리에서 .cshtml파일을 추가하는 방법은 없나요? [3]
5063진우9/28/201817574ADO.net 과 Entity Framework 차이 문의 [2]
5062테스트9/27/201818058C# import file 의 구조체 배열 선언 및 호출에 대해 문의. [3]
5061안녕하세요9/13/201818831c# 프로그래밍 관련 문의 [1]
5060임민재9/8/201817892c# install 파일 생성 시 문제가 발생하였습니다 [1]파일 다운로드1
50599/7/201817200Winform TextBox 포커스 유지하는 방법 질문 [파일첨부] [1]파일 다운로드1
50589/5/201821833Winform TextBox 포커스 유지하는 방법 질문 [3]
5056박종윤8/30/201820541c# dll을 C++에서 사용 시 event 호출 [4]파일 다운로드1
5055초보자8/29/201818975asp.net 에서 다른 서버의 iis를 stop하는 batch file을 실행시키는데 동작하지 않습니다. [5]
... 31  32  [33]  34  35  36  37  38  39  40  41  42  43  44  45  ...