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

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)
5413민석11/20/202018770C# minidump를 프로그램이 중단 될 때 만들고 싶습니다. [1]파일 다운로드2
5411원격11/20/202016287visualstdio로 웹 사이트로 만들었을때 원격 디버깅이 가능한가요? [1]
5410최성재11/16/202016829vcpkg로 GDCM 내려받을 때 USE_VTK 설정하는 방법-2번째 질문 [1]파일 다운로드1
5409민성11/16/202019567혹시 다른 질문이긴 한데요 [1]
5408최성재11/16/202016432vcpkg로 GDCM 내려받을 때 USE_VTK 설정하는 방법 [1]
5407민성11/11/202014405안녕하세요 yield return에 대해서 [1]
5406질문자11/10/202015646안녕하세요 wcf nettcpbinding의 timeout에 관해서 질문이 있습니다. [2]
5405민성11/9/202015347안녕하세요 이번에도 또 어려운 질문 같습니다. [1]
5404박진우11/6/202018404안녕하세요. SqlParameter 생성자 관련 질문 있습니다. [1]
5403민성11/5/202018114그리고 한가지만 죄송하지만 더 질문 드리겠습니다. [1]
5402민성11/5/202018080안녕하세요 책을 보고 질문하나만 드릴깨요 [2]
5401민성11/3/202016946안녕하세요 이번에도 질문 하나만 드리겠습니다. [2]
5400진우10/29/202016710SQL Server 관련 몇가지 문의 [2]
5399Wpf개...10/21/202016823Binding 된 항목의 갱신 시 간헐적 끊어짐 발생 문제. [2]
5397나그네10/15/202015936.net Core 3.1 에서 Entity Framework 와 ADO.NET 선택에 관해 여쭤봅니다. [2]
5396여정욱10/15/202015589CLR heap 관련 질문 2 [2]
5395여정욱10/14/202018391CLR heap 관련 질문 [2]
5394진우10/12/202022414닷넷코어 (닷넷5) winform wpf는 리눅스/맥에서도 가능한가요? [2]
5393김세용9/23/202017692C#에서 대량의 클래스를 빠르게 생성하는 방법이 없을까요? [6]
5392전경호9/22/202017245WPF에서 WindowsFormsHost의 메모리 누수 문제 때문에 문의드립니다. [1]파일 다운로드1
5391민성9/22/202016564안녕하세요 항상 감사드립니다. 하나 질문 드리겠습니다. [1]
5390alow...9/18/202019726System.AccessViolationException 보호된메모리 부분 예외처리 [1]
5389C# 8...9/18/202016409후위 증감 연산자 오버로딩 방법 좀 알려주세요 [4]
5388영귤9/17/202020594Nullable reference type 에 Non-nullable reference type 을 대입해도 경고가 발생하지 않습니다. [2]
5387하태9/17/202017283안녕하세요! 비동기 통신과 관련하여 질문하나만 드리겠습니다! [3]
5386박민웅9/16/202020415정성태 스승님 안녕하세요 !! [1]
... 16  17  18  19  20  21  [22]  23  24  25  26  27  28  29  30  ...