출저 및 참고 : http://blog.eairship.kr/151 (누구나가 다 이해할 수 있는 프로그래밍 첫걸음 블로그)
를 보면서 공부하고, 나중에 다시 보기 편하게 여기에 정리해 놓은 것입니다. 원본은 위 글입니다~!
델리게이트
- 대리자, 메소드 참조를 포함하고 있는 영역
- 선언 형식
| 1 | delegate 반환형 델리게이트명(매개변수..); | cs | 
- 사용 예제
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication39 {     delegate int PDelegate(int a, int b);     class Program     {         static int Plus(int a, int b)         {             return a + b;         }         static void Main(string[] args)         {             PDelegate pd1 = Plus;             PDelegate pd2 = delegate(int a, int b)             {                 return a / b;             };             Console.WriteLine(pd1(5, 10));             Console.WriteLine(pd2(10, 5));         }     } } | cs | 
결과 : 15
2
델리게이트 체인
- 델리게이트 하나를 가지고 여러개의 메소드를 한 번에 호출 가능
- Delegate.Combine 메소드 : 2개 이상의 대리자의 호출 목록을 연결
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication39 {     delegate void PDelegate(int a, int b);     class Program     {         static void Plus(int a, int b)         {             Console.WriteLine("{0} + {1} = {2}", a, b, a + b);         }         static void Minus(int a, int b)         {             Console.WriteLine("{0} - {1} = {2}", a, b, a - b);         }         static void Division(int a, int b)         {             Console.WriteLine("{0} / {1} = {2}", a, b, a / b);         }         static void Multiplication(int a, int b)         {             Console.WriteLine("{0} * {1} = {2}", a, b, a * b);         }         static void Main(string[] args)         {             PDelegate pd = (PDelegate)Delegate.Combine(new PDelegate(Plus),                 new PDelegate(Minus), new PDelegate(Division), new PDelegate(Multiplication));             pd(20, 10);         }     } } | cs | 
결과 : 20 + 10 = 30
20 - 10 = 10
20 / 10 = 2
20 * 10 = 200
이벤트
- 특정 행동이 발생하면 알리는 메시지
- 이벤트 발생 ex) 사용자가 컨트롤(버튼, 이미지, 레이블, 텍스트박스 등)을 클릭하거나 창을 닫거나 열때 사용자에게 알림
- 선언 형식
| 1 | 한정자 event 델리게이트 이름; | cs | 
- event 사용방법
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication42 {     public delegate void MyEventHandler(string message);     class Publisher     {         public event MyEventHandler Active;         public void DoActive(int number)         {             if (number % 10 == 0)                 Active("Active!" + number);             else                 Console.WriteLine(number);         }     }     class Subscriber     {         static public void MyHandler(string message)         {             Console.WriteLine(message);         }         static void Main(string[] args)         {             Publisher publisher = new Publisher();             publisher.Active += new MyEventHandler(MyHandler);             for (int i = 1; i < 50; i++)                 publisher.DoActive(i);         }     } } | cs | 
결과 : 1
..
Active!10
..
Active!40
..
49
'NOTE > Programming' 카테고리의 다른 글
| [C#] 확장 메소드, 분할 클래스, 중첩 클래스 (0) | 2018.04.01 | 
|---|---|
| [C#] 클래스의 상속 (0) | 2018.04.01 | 
| [C#] Dictionary<Tkey, TValue> 클래스 (0) | 2018.04.01 | 
| [C++] 문자열을 입력받는 12가지 방법 (펌) (0) | 2018.04.01 | 
| [C#] 열거형 형식 (0) | 2016.11.08 |