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

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)
5644ㅇㅇ4/6/20224257c# 프로그램을 이용하여 리눅스상에 파일 생성이 가능한가요? [1]
5643유필재4/5/20224132TCP클라이언트 연결 및 통신관련하여 문의드려요 [1]
5642차가워4/4/20224387UdpClient 패킷 수신 문의 [4]
5641장성욱4/4/20224786코어 할당 및 cpu 부하테스트 질문 [7]
5640icoo...4/4/20224312웹가든에서 메모리 동적 업데이트 방법 [1]
5639차가워4/4/20224232c++ 서버 c# 클라이언트 호환 문의 [1]
5638초급4/3/20224538c# sql server 연동 [1]
5637따봉이4/1/20224802Winform Form Load 후 자동 캡쳐관련 [1]파일 다운로드1
5636김철순3/31/20224770WPF에서 Richtext의 View 문의 [5]
5635guest3/30/20224761안정적인 pinning이 가능하네요. [3]파일 다운로드1
5633꿀주세요3/30/20224381선생님 마우스 클릭이벤트 질문이 있습니다. [4]
5632김현수3/30/20224757Remote Desktop으로 접속시 WPF UI 가 다시 그려지는 이벤트를 막을 수 없을까요? [3]
5631김기헌3/24/20224365WPF 컨트롤의 그래픽 처리관련 질문드립니다 [2]파일 다운로드1
5630장성욱3/24/20224178로깅관련 질문입니다. [2]
5629감사합니...3/23/20224418함수에서 예외가 발생하면 try ~ catch처리기를 찾을 때 까지 상위 함수로 계속 올라가나요? [2]
5628홍길동3/23/20225072질문드립니다. [2]파일 다운로드1
5626연준혁3/21/20224325안녕하세요. [3]
5625jaew...3/18/20225270c# 8.0 도서를 구입한 사람입니다. [1]
5624초보자3/17/20224230람다 캡처 관련 문의 [2]
5623한예지 donator3/15/20224636인터프리터 원리가 궁금합니다. [4]
5622김민아3/8/20224543const와 readonly의 명확한 차이가 이게 맞나요? [2]
5621장성욱3/8/20224320c# 로그 관련 질문 [1]
5620김민아3/7/20224356안녕하세요 비관리 객체를 반환하는 메소드 호출 시 궁금한 점이 있어서 질문드립니다 [2]
5619팡팡이3/3/20225715RSA 문의드립니다. [3]
5618김기헌3/2/20224222안녕하세요 생성자 옆에 this 키워드를 붙여 생성자를 여러 개 호출 시 질문드립니다 [2]
5617Edun2/25/20224349ArgumentOutOrRangeException에러 발생 [2]파일 다운로드1
1  2  3  4  5  6  7  8  9  10  11  [12]  13  14  15  ...