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

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)
5565pass...10/25/20216497wpf에서 Cefsharp를 AnyCPU로 작업 했는데 실행이 되지 않습니다. [4]
5564초보10/21/20214738오버라이드 관련 질문드립니다 [2]
5562서지훈10/20/20214747win32 api 문의 드립니다. (EnableMenuItem) [5]
5561Edun10/15/20215173도와주세요!! 팝업 띄우는 더블클릭 이벤트!! 부탁드립니다 ㅠㅠ [4]파일 다운로드1
5560Edun10/13/20215368C#초보자 개념이해 도와주세요!! [4]
5559최가운10/5/20215668C# debugging 중에 Thread Pool Worker들이 사라지는데요 [3]
5558옥코드9/29/20215936안녕하세요! EntityFramework Linq 질문 드리겠습니다. [4]
5557임기성9/16/20216494웹 사이트(IIS)가 오류로 인하여 빈번하게 죽는 이슈 문의 [1]
5556hjlee9/13/20215293visual studio 설치 없이 csi.exe (C# interactive) tool을 설치할 수 있을까요? [2]
5555이성열 donator9/2/20215115C# 런타임(Reflection)에서 소스코드에 입력한 함수설명(Summary)을 가져올 수 있을까요? [3]
5554농상9/2/20215800enum 원소값이 중복인 경우 출력 [4]
5553shdt...8/31/20214774제너릭을 new로 할당했는데 null인 경우가 존재하나요? [1]
5552hjsh...8/30/20215044C# 폼 - 마우스가 다른 버튼 위를 움직일 때도 폼의 페인트핸들러가 호출되는 것이 맞나요? [2]
5551하나다라마8/27/20215167뽀모도로 타이머 개선사항 [1]
5550하영8/27/20216256SqlDataReader 관련 질문입니다. [8]
5549책 감사...8/23/20217676C# 9.0 책 오타 [1]
5548Dev8/23/20215970String의 IndexOfAny 함수 질문 [3]
5547민성8/20/20215389WPF Radio 버튼을 GroupName으로 읽어와서 비교하기 [1]
5546Deve...8/19/20216252C++ C# API 연동 관련 [1]
5545영귤8/18/20215180C# 14.3장 예제에서 반환 값의 값 복사 부하가 어디서 발생하는 것인가요? [4]
5544이상호8/18/20215511C# Graphics 객체 저장 가능 여부 [2]
5543정형지8/18/20215277c# outofmemoryexception [3]
5542영귤8/11/20217995C#9.0 886쪽 오류 발생 안 해요 [2]
5541서지훈8/11/20218733C# 응용프로그램(Winform)의 비정상 종료(Exit Code) [2]
5540산들마을...8/11/20216302C# 많은 Control(Label 300개) Update 관련 문의 [2]파일 다운로드1
5539초초초촙8/9/20214816c# textbox 입력값이 다름 [1]파일 다운로드1
1  2  3  4  5  6  7  8  9  10  11  12  13  14  [15]  ...