반응형
델리게이트
- 대리자
- 메소드의 틀을 만들어 소통
- 클래스간 통신에 활용
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /*----------------------------------------------------------------------------- * Name: _delegate * DESC: delegate 기초 -----------------------------------------------------------------------------*/ namespace _delegate { delegate int DelegateFunc(int a); class Program { static int Add(int a) { Console.WriteLine("Add"); return a + a; } static void Main(string[] args) { DelegateFunc delegateFunc = Add; //delegateFunc(Add) Console.WriteLine("delegateFunc: " + delegateFunc(10)); } } }
델리게이트 선언 방법
- 기본 선언
- 간략한 선언
- 익명 함수 선언
- 람다식 선언
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /*----------------------------------------------------------------------------- * Name: _delegateDeclare * DESC: delegate 선언 방법 -----------------------------------------------------------------------------*/ namespace _delegateDeclare { delegate void DelegateTest(int a, int b); // 1, 2번은 미리 함수를 선언해서 사용하는 방법 // 3, 4번은 바로 함수를 작성해서 사용하는 방법 class Program { static void Sum(int a, int b) { Console.WriteLine("a + b = " + (a + b)); } static void Main(string[] args) { //1: 기본 선언 DelegateTest dt = new DelegateTest(Sum); //2: 간략한 선언 DelegateTest dt2 = Sum; //3: 익명 함수 선언 DelegateTest dt3 = delegate (int a, int b) { Console.WriteLine("a + b = " + (a + b)); }; //4: 람다식 선언 DelegateTest dt4 = (a, b) => { Console.WriteLine("a + b = " + (a + b)); }; dt(1, 1); dt2(2, 2); dt3(3, 3); dt4(4, 4); dt = delegate (int a, int b) { Console.WriteLine("a - b = " + (a - b)); }; dt(2, 1); } } }
델리게이트 함수 파라미터 활용
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /*----------------------------------------------------------------------------- * Name: _delegateParam * DESC: delegate 파라미터 -----------------------------------------------------------------------------*/ namespace _delegateParam { delegate void delegateFunc(); class MessageProcess { delegateFunc CallOkFunc; delegateFunc CallCancelFunc; public void Message(string msg, delegateFunc okFunc, delegateFunc cancelFunc) { CallOkFunc = okFunc; CallCancelFunc = cancelFunc; Console.WriteLine("Message: " + msg + " (0: ok, 1: cancel)"); string inputStr = Console.ReadLine(); if(inputStr.Equals("0")) { CallOkFunc(); } else { CallCancelFunc(); } } } class Program { static void CallOK() { Console.WriteLine("CallOK"); } static void CallCancel() { Console.WriteLine("CallCancel"); } static void Main(string[] args) { MessageProcess msg = new MessageProcess(); // 1. 일반적인 사용 방법 //msg.Message("Test Message", CallOK, CallCancel); // 2. 내부에 익명 함수로 사용하는 방법도 존재 msg.Message("Test Message", delegate () { Console.WriteLine("Call InDelegate"); } ,CallCancel); } } }
반응형
'IT > C#' 카테고리의 다른 글
[C#_List] Find 검색 (0) | 2021.10.01 |
---|---|
[C#_기본] 람다식 - Lambda Expression (0) | 2021.10.01 |
[C#_클래스] this, static 키워드 (0) | 2021.09.28 |
[C#_클래스] 개념과 생성자, 소멸자 (0) | 2021.09.28 |
[C#_기본] 접근제한자 (0) | 2021.09.28 |