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

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)
5002멋쟁이7/2/201818546WPFApp에 관한 초보 질문입니다. [1]파일 다운로드1
5001김학완7/1/201818313시작하세요 C# 7.1 프로그래밍 P53쪽 질문입니다. [2]
5000레몬6/26/201820427안녕하세요 성태님 도움으로 C# 네이버 카페 스팸글 작성되면 삭제되는 프로그램을 만들었는데요..여쭤볼게 하나 있습니다. [3]
4999jt6/21/201815587검색어 입력, 엔터 > 페북 로그인창으로 이동합니다. [4]
4998개발희망6/11/201818129C# 형변환 질문있습니다! [1]
4997초보개발자6/5/201832390C++ dll C#에서 사용하는데 보호된 메모리 오류떠요 한번만 도와주세요ㅜ [1]파일 다운로드1
4996swc6/5/201818212현업에서 주로 사용하는 DB업데이트 방법 질문입니다. [1]파일 다운로드1
4993학생6/3/201816304질문 드립니다 [1]
4991최진안5/24/201816889Credential Provider 질문 [2]
4990heyg...5/21/201817031Sybase Adaptive Server Anywhere 6 버전에 대한 질문입니다. [9]
4989강한음5/18/201820129clickonce 배포 후 실행 무응답 [4]
4988C#7....5/17/201815464dynamic 키워드를 사용한 객체 핸들링에 대해서 여쭤보려고 합니다. [1]
4987포플러5/14/201818021닥터왓슨 로그 - c0000005 (액세스 위반) 분석 부탁드려도 될까요? [2]
4986익명5/14/201819868비주얼 스튜디오 wpf 프로젝트에서 어떻게 하면 exe파일과 실행에 필요한 파일들을 분리해서 정리해서 디렉토리로 묶을 수 있을까요? [4]
4985대구개발자5/7/201817331새로운 폴더만 "이름 없는 파일" 오류 [1]
4984안중언5/6/201817834교재 143page [1]
4983익명5/4/201828691(wpf) 다른 컴퓨터에서 사용하면 자꾸 ('CefSharp.Core.dll' 또는 여기에 종속되어 있는 파일이나 어셈블리 중 하나를 로드할 수 없습니다)라고 떠요.ㅠㅠ [5]
4982Soul...4/27/201818285MFC ActiveX 컨트롤 안에 있는 C# ActiveX 컨트롤 포인터 얻기 [4]
4981대전박4/25/201816937WPF IValueConverter 를 구현해서 StaticResource로 사용할때요 [1]
4980대전박4/23/201818776WPF OS버전 따라 Style 적용이 안되는 프로퍼티가 있을수 있나요? [2]
4979초보개발자4/18/201823480C# 프레임워크 버전이 다른 DLL끼리의 사용 [7]파일 다운로드1
4977Soul...4/17/201818213WebBrowser 컨트롤 Script 통신 문제 [3]
4976맹가이버4/14/201819265윈도우 서비스 프로그램에서 응용프로그램 호출하는 법 [1]
4975lemo...4/11/201819610안녕하세요 네이버로그인관련 질문드립니다. [2]
4973홍길동4/6/201817361ebook 출간 계획은 없으신가요? [2]
4978홍길동4/17/201816958    답변글 [답변]: ebook 출간 계획은 없으신가요?
... 31  32  33  34  35  [36]  37  38  39  40  41  42  43  44  45  ...