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

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]

... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
5132존기미3/3/201914640Physical Disk Access문의 [1]
5131강용규2/25/201913933c# 개발자입니다. 타이머 질문드립니다. [1]
5130노인코래방2/25/201918165C#에서 가장 좋은 성능을 보이는 파일 읽고 쓰는 방법이 무엇인가요? [2]
5129진우2/23/201917910닷넷 32비트 기반에서 메모리 부족으로 프로그램이 죽는경우 문의 [2]
5128게스트2/23/201915104안녕하세요. 초보개발자입니다. [3]파일 다운로드1
5127c#2/20/201913990책에 예제 문의드립니다. [2]
5125게스트2/19/201913819delegate를 활용한 event 를 적절히 불러오고 싶습니다. [2]
5124정근화2/12/201913866윈도우 서버2003 환경 오류 [2]
5123김주현2/8/201913692MS LUIS 에 대한 소개 하실 계획이 있으신가요? [1]
5122jaka...2/1/201915782Clickonce 배포 후 Command 실행 [2]파일 다운로드1
5121엔벌이1/31/201915409C# DataGridView의 MDB파일 함수? ArrayList? [1]파일 다운로드1
5120임우진1/30/201916556웹에서 응용프로그램 바로 실행하기 관련 브라우저에서 파라미터가 넘어오지 않습니다.ㅜㅜ [2]
5119guest1/29/201918134교재에 오탈자 있어 알려드리려 합니다 [1]
5118WPF꿈...1/26/201916135GetHashCode 메서드에 대해서 [1]
5117하주형1/25/201915907List<int>에 대한 이해가 잘안됩니다. [5]
5116게스트1/24/201914448asp.net 관련 gridview webform 질문 드립니다. [1]파일 다운로드1
5115Soul...1/24/201914672투명 패널 질문드립니다. [2]
5114박현일1/20/201915236WPF DataContext 관련 초보 질문을 드려봅니다.^^ [5]
5113하주형1/20/201914504안녕하세요 시작하세요 C# 인코딩관련 질문드립니다. [1]
5112손성배1/19/201923838안녕하세요 cp949 인스톨시 오류입니다... 너무 힘들어요 [5]
5111게스트1/10/201914989암호화 라이센스 관련 문의 드립니다. [1]
5110WPF꿈...1/9/201914474Thread Abort 함수 사용시 [2]
5109닷넷개발1/9/201914249thread 관련 질문 예제.. [2]파일 다운로드1
5108닷넷개발1/9/201915465thread 관련 질문 드립니다.. [4]
5107우코아1/4/201918216WPF에서 로딩중 이미지를 구현 - Project [5]파일 다운로드1
5106우코아1/3/201915851WPF에서 로딩중 이미지를 구현 - Source [1]
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...