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

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)
5885guest4/11/20233032c#으로 드림위버같은 거 만들어볼려는데요 [6]
5884궁금이4/11/20232908부모 클래스에서 예외 발생시 힙 영역에 할당 ? [2]
5883코딩초짜4/9/2023282610c언어 usleep 에 대해서 요 [2]
5882조은현4/7/20232839선생님 안녕하세요! wpf의 성능 개선에 대해서 질문드려요! [1]파일 다운로드1
5881guest4/6/20232984static method - <에러메시지 Extension method must be defined in a non-generic static class> [4]
5880유비4/4/20232858WPF DataGrid CollectionView, IEditableCollectionView 관련 문의 [1]
5879guest4/4/20233074Async method의 에러 표시 [3]
5878guest4/3/20233168C#으로 CMOS 설정 변경가능한지요? [4]
5875guest4/2/20233580성태님 책을 완독 하고 Static [7]
5874민성4/1/20232979안녕하세요 질문 하나만 드릴깨요~ [1]
5873guest3/31/20233229제어판에서 삭제불가 MS Edge ---> 레지스트리 편집기에서도 안보임 [6]파일 다운로드1
58723/31/20232877web config 파일 확인부탁드려요 [6]
58713/31/20232790web config 파일 수정이요 [2]파일 다운로드1
5870guest3/30/20233269.NET Core SDK 삭제 시 주의 사항 [4]파일 다운로드1
5869guest3/30/20233494Dictionary의 Update 그리고 Foreach [7]
5868guest3/29/20232970Speech Recognition과 Form1 그리고 정확도 [4]파일 다운로드1
5866월급쟁이3/28/20233063cmake 크로스 컴파일 관련하여 질문이 있습니다 [1]
5865guest3/28/20232936Github Copilot과 코딩실력 향상? [1]
5864guest3/27/20233362System.NullReferenceException - 개체참조가 개체의 인스턴스... [6]파일 다운로드1
5863guest3/24/20233489이벤트 핸들러 사라짐 현상 - Button [4]
5862guest3/21/20233564세계최초 hts와 싱글스레드 [8]
5861다크파이썬3/21/20233729WPF를 사용하려고 하려고 도서 문의합니다. [2]
5860guest3/21/20233012인텔코어 i5 CPU와 스레드 [4]
5859guest3/21/20232853개발 일지 어떻게 관리하시나요? 이런 프로그램 없나요? [3]
5858김태원3/18/20232912안녕하세요! [5]
5857guest3/17/20232986귀도 반 로썸을 보고 [4]
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...