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

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)
5125게스트2/19/20196957delegate를 활용한 event 를 적절히 불러오고 싶습니다. [2]
5124정근화2/12/20196829윈도우 서버2003 환경 오류 [2]
5123김주현2/8/20196874MS LUIS 에 대한 소개 하실 계획이 있으신가요? [1]
5122jaka...2/1/20198715Clickonce 배포 후 Command 실행 [2]파일 다운로드1
5121엔벌이1/31/20198132C# DataGridView의 MDB파일 함수? ArrayList? [1]파일 다운로드1
5120임우진1/30/20198631웹에서 응용프로그램 바로 실행하기 관련 브라우저에서 파라미터가 넘어오지 않습니다.ㅜㅜ [2]
5119guest1/29/201910022교재에 오탈자 있어 알려드리려 합니다 [1]
5118WPF꿈...1/26/20197949GetHashCode 메서드에 대해서 [1]
5117하주형1/25/20198611List<int>에 대한 이해가 잘안됩니다. [5]
5116게스트1/24/20197365asp.net 관련 gridview webform 질문 드립니다. [1]파일 다운로드1
5115Soul...1/24/20197549투명 패널 질문드립니다. [2]
5114박현일1/20/20197899WPF DataContext 관련 초보 질문을 드려봅니다.^^ [5]
5113하주형1/20/20197145안녕하세요 시작하세요 C# 인코딩관련 질문드립니다. [1]
5112손성배1/19/201916171안녕하세요 cp949 인스톨시 오류입니다... 너무 힘들어요 [5]
5111게스트1/10/20197783암호화 라이센스 관련 문의 드립니다. [1]
5110WPF꿈...1/9/20197424Thread Abort 함수 사용시 [2]
5109닷넷개발1/9/20197392thread 관련 질문 예제.. [2]파일 다운로드1
5108닷넷개발1/9/20198544thread 관련 질문 드립니다.. [4]
5107우코아1/4/201911193WPF에서 로딩중 이미지를 구현 - Project [5]파일 다운로드1
5106우코아1/3/20198824WPF에서 로딩중 이미지를 구현 - Source [1]
5104우코아1/1/201910059WPF에서 로딩중 이미지를 구현 [4]
5103이혜성12/31/20181064432bit .net 으로 만들어진 dll파일 [5]
5102돌고래12/18/20189415자료구조와 알고리즘 도서 관련 질문입니다. [4]
5101세퉁12/17/20187738안녕하세요 wpf 공부중인데 질문있습니다. [4]파일 다운로드1
5100돌고래12/16/20187101도서 추천 부탁드립니다. [1]
5099WPF12/12/201810178안녕하세요. WPF에서 UWP Control을 참조하려고 합니다. [3]파일 다운로드1
... 16  17  18  19  20  21  22  23  24  25  26  27  28  29  [30]  ...