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

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]

... 61  62  63  64  65  66  67  68  69  70  71  72  73  [74]  75  ...
NoWriterDateCnt.TitleFile(s)
694이호정8/29/200714167[CardSpace 관련 질문] 정리해 좋으신 글 잘 보았습니다.
698정성태9/1/200714344    답변글 [답변]: [CardSpace 관련 질문] 정리해 좋으신 글 잘 보았습니다.
704이호정9/6/200715240        답변글 답변해주신 내용 잘 봤습니다. ^_^b
705정성태9/6/200713589            답변글 [답변]: 답변해주신 내용 잘 봤습니다. ^_^b
706이호정9/7/200714349                답변글 후후... 이러다가 CardSpace 전용게시판 되는 것 아닌지 모르겠습니다. ^_^a;;
693장근배8/29/200713866매핑 구간이 열려 있습니다 [2]
710장근배9/17/200713580    답변글 [답변]: 매핑 구간이 열려 있습니다
692채영사랑8/29/200713494<질문> Ms stress tool 로 부하테스트중.
697정성태9/1/200713231    답변글 [답변]: <질문> Ms stress tool 로 부하테스트중. [1]
690한귀순8/28/200714208ClickOnce 배포 추가질문 [4]
689clev...8/27/200713604SmartClient에서도 Exe COM과 같이 여러곳에서 변수 공유가 가능한가요? [2]
688한귀순8/24/200716066ClickOnce 배포 [1]
686이상욱8/21/200715012[질문] VS.NET 2008 - WCF 를 위한 디버깅 환경 개선 테스트 중에
687정성태8/21/200713319    답변글 [답변]: [질문] VS.NET 2008 - WCF 를 위한 디버깅 환경 개선 테스트 중에
685김동진8/19/200715607Vista에서 WSDL관련 IIs7세팅 질문입니다. [4]
683조스7/31/200714713스마트 클라이언트에서 다른 어셈블리(.NET component) 사용시 에러 [1]
682정해봉7/30/200715825비스타에서 CAS설정하기... 소스 첨부 [1]파일 다운로드1
684정해봉8/14/200716511    답변글 [답변]: 비스타에서 CAS설정하기... 소스 첨부 [3]
681박희진7/27/200714458caspol로 설정된 권한집합 웹에서 읽어올 수 있나요?? [1]
680창민이7/27/200715160COM+등록시 에러 부분 때문에.. [2]
678정해봉7/24/200713997비스타에서 CAS 설정하기... [1]
677첫사랑7/24/200713306SSL에 관한 질문입니다.
679정성태7/26/200713974    답변글 [답변]: SSL에 관한 질문입니다. [1]
676kiuk7/11/200714320웹폐이지상의 이미지 상태 저장을 하고싶습니다. [1]
675안연준7/6/200714823안녕하세요 ^^ [1]
674황태욱7/5/200716596utf-8 환경에서 파라미터 값에 한글이 있을 경우 깨지는 현상이.. [1]
... 61  62  63  64  65  66  67  68  69  70  71  72  73  [74]  75  ...