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

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)
5251kss11/1/20196845c#문법질문입니다. [1]
5250이대희10/31/20196938시작하세요 C# 7.3 프로그래밍 책이 절판이네요. [1]
5249황태관10/24/20197393c# 폼안에 특정 프로그램 불러오기.. [1]파일 다운로드1
5248황태관10/23/20198272C언어로 만든 dll 를 참조해서 c#으로 만들려면.. [3]
5245민성10/21/20197417안녕하세요 Ui 스레드 접근에 대해서 질문드리도록 하겠습니다. [1]
5244hbli...10/20/20196822두개의 쓰레드에서 하나의 Queue 리스트 사용시 질문드립니다. [2]
5243최선호 donator10/18/20196364Screen Capture 와.. Overlay 질문 좀 드릴게요 ㅠ.ㅠ ( 장문 주의..... ) [2]
5241C#초보10/16/20198405C# USB 통신 구현 관련 질문 드립니다! [1]
5240김동규10/15/20198375런타임 시 Encoding.Default를 UTF-8로 설정할 수 있을까요? [3]
5239밤톨이10/13/20196633시놀리지 DSM에 asp.net core 2 웹서버 구축이 가능한가요? [1]
5238권석헌10/2/20197165C# - C++ 간의 Memorymapped file 관련 Data 질문좀 드립니다 선생님 [1]
5237유호성10/1/20196913사용자 정의 컨트롤 VS IDE에서만 Exception 발생 [2]
5234koko9/25/20199153안녕하세요. 64bit WPF -> 32bit C++ DLL Import사용 문의 드립니다. [3]
5232fox3...9/24/20197021C# Memorymappedfile 재 업로드 드립니다.(실행순서 및 질문내용 정리) [7]파일 다운로드1
5231fox3...9/24/20197449C# 메모리맵드파일 관련 질문드립니다. 최소한의 재현가능코드 첨부후 재질문 드립니다. [2]파일 다운로드1
5230질문9/23/20197108C++ 로 만든 dll 을 C#에서 사용할 때 질문 [2]
5229fox3...9/23/20199472C# 메모리맵드파일 관련 질문드립니다. [2]
5227세퉁9/23/20198122WPF Textblock 폰트 크기에 따라 글자 색이 깨지는 현상이 있습니다. [3]파일 다운로드1
5226김대훈9/23/20197775정말 황당한 경우입니다.. [2]
5223김태균9/19/20197508책 소개 링크가 7.1버전판으로 이어집니다. [1]
5222냥냥이9/14/20198044프로그래밍 논리력이 많이 부족합니다 [3]
5219티지레몬9/9/20199519c# PCB 자동화 프로그램(윈도우 폼 위주로 작업) 제작 준비 [3]
5218민성9/9/20197588안녕하세요 WPF에서 xaml 안에 다른 xaml을 넣고 싶습니다. [1]파일 다운로드1
5216WPF9/8/20198670WPF에서 XAML Islands를 사용하여 Win2D를 사용하니 그래픽 품질이 저하됩니다. [2]파일 다운로드1
5215허송세월9/5/20198479중복실행 방지 관련 문의 [2]파일 다운로드1
5214Jang...9/4/20198085[DB 테이블의 데이터 변경에 대한 알림 처리] SQL-Server말고 MySQL은 불가능하겠죠? [1]
... 16  17  18  19  20  21  22  23  24  25  [26]  27  28  29  30  ...