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

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)
5597재원2/2/202212331c# 9.0에 대한 내용을 받을 수 있나요? [1]
5596kss1/26/202211943듀얼 모니터 환경에서 wpf 프로그램 크기 변경 [2]
5594mira...1/25/202211237안녕하세요 try~catch 와 SuspendLayout~ResumeLayout 간 호출 문의드립니다! [2]
5593C#스터디1/12/202212584TaskAwaiter 구현 질문 입니다. [1]
5591유지킴12/24/202112132outofmemory in 32bit [2]파일 다운로드1
5590kss12/23/202111158포인터 메모리 에러 [1]
5589초급12/22/202111380c# -> 라즈베리파이(db 접속)시 에러 발생 [7]
5588김지신12/21/202111627안녕하세요 String 변수 참조 주소 질문입니다. [2]
5587이완호12/17/202113163C# SharpDX 화면 캡쳐 관련해서 질문 드립니다. [1]
5586조미김12/16/202111997윈폼에서 메인 스레드와 UI 스레드의 차이점 질문드립니다 [4]
5585김준희12/16/202112983C# 윈폼 TCP/IP 데이터 연속으로 보낼때 [1]
5583난인간이다12/10/202111348.net 6 dynamic pgo 활성화 및 예상되는 문제 [1]
5582김준희12/9/202121642C# TCP/IP 통신시 연결 끊김 에러 [1]
5581김시준12/9/202112623닷넷에서 파일 delete 함수는 왜 비동기가 없는 것인가요? [2]
5580카짜프로...12/7/202114327패턴매칭 -튜퓰비교에 관한 오류사항과 궁금증 [1]파일 다운로드1
5579카짜프로...12/6/20211352211.12 메서드 중복정의에 대한 질문 [1]
5577감사합니...11/30/202112397visual studio 2015 update 3를 다운받을려고 하는데 x64/x86 차이점이 뭘까요? [2]
5576노홍구11/29/202112277C# 으로 USB 스캐너 프린터 리셋하기 입니다. [1]
5575베라11/23/202112058event handler 관련 문의 [2]
5574박원웅11/22/202112590닷넷 프레임워크 산출물의 배포시 해당 환경에 프레임워크 버전이 설치되어 있지 않는 경우를 고려한 배포방법은? [3]
5573mijin11/21/202111865System.NullReferenceException 에 대한 질문 [1]
5572김현진11/21/202111892C# list.Clear() 호출에 대한 문의 드립니다. [4]
5571한예지 donator11/18/202112024무설치 프로그램 원리가 궁금합니다. [2]
5570초보11/16/202112559주식데이터 초당 수신 건수를 구하고 있는데 처리속도가 느려서요 [2]
5569카짜프로...11/14/202114103381페이지 UTC에대한 언급이 그리니치 천문대 시간으로 되어있는게 맞나요? [1]
5568카짜프로...11/14/202114171그림 5.20, 그림 5.22 언급 오류 [1]
1  2  3  4  5  6  7  8  9  10  11  12  13  14  [15]  ...