VioletaBabel
6일차 본문
델리게이트
메소드에 대한 참조로, 콜백 구현. 델리게이트에 메소드의 주소를 할당 후 호출하면 델리게이트가 메소드를 호출해 줌.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | delegate int MyDelegate(int a, int b); class Calc { public int Plus(int a, int b) { return a + b; } public static int Minus(int a, int b) { return a - b; } } class Program { static void Main(string[] args) { Calc c = new Calc(); MyDelegate cb; cb = new MyDelegate(c.Plus); Console.WriteLine(cb(3, 4)); cb = new MyDelegate(Calc.Minus); Console.WriteLine(cb(4, 3)); } } | 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 31 32 33 34 35 36 37 | static int AscendCompare(int a, int b) { if (a > b) return 1; if (a == b) return 0; else return -1; } static int DescendCompare(int a, int b) { if (a < b) return 1; if (a == b) return 0; else return -1; } static void Bubble(int[] Data, Comp c) {//델리게이트를 매개변수로 사용 int i = 0, j = 0, t = 0; for(i = 0; i < Data.Length-1; ++i) for(j = 0; j < Data.Length-(i+1);++j) if(c(Data[j],Data[j+1])>0) { t = Data[j + 1]; Data[j + 1] = Data[j]; Data[j] = t; } } static void Main(string[] args) { int[] ar = { 3, 7, 6, 1, 9 }; Bubble(ar, new Comp(AscendCompare));//매개변수로 델리게이트를 넣음 foreach (int i in ar) Console.WriteLine(i); Console.WriteLine("---"); int[] ar2 = { 5, 3, 6, 8, 9 }; Bubble(ar2, new Comp(DescendCompare)); foreach (int i in ar2) Console.WriteLine(i); } | 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 31 32 33 34 | static int AscendCompare<T>(T a, T b) where T : IComparable<T> {//IComparable<T>는 CompareTo()메소드를 쓰기 위한 것. return a.CompareTo(b); } static int DescendCompare<T>(T a, T b) where T : IComparable<T> {//모든 수치 형식과 string은 IComparable을 상속하여 CompareTo()를 구현함. return a.CompareTo(b) * (-1); } static void Bubble<T>(T[] Data, Comp<T> c) { int i = 0, j = 0; T t; for(i = 0; i < Data.Length-1; ++i) for(j = 0; j < Data.Length-(i+1);++j) if(c(Data[j],Data[j+1])>0) { t = Data[j + 1]; Data[j + 1] = Data[j]; Data[j] = t; } } static void Main(string[] args) { int[] ar = { 3, 7, 6, 1, 9 }; Bubble(ar, new Comp<int>(AscendCompare)); foreach (int i in ar) Console.WriteLine(i); Console.WriteLine("---"); string[] ar2 = { "abr","ayc","bac","abc","brt" }; Bubble(ar2, new Comp<string>(DescendCompare)); foreach (string i in ar2) Console.WriteLine(i); } | cs |
델리게이트 체인
델리게이트 하나가 여러 메소드를 동시에 참조할 수 있다. +, =, += 연산자를 사용하거나 Delegate.Combine() 메소드 이용
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 42 43 44 45 46 47 48 49 | delegate void Ntf(string m); class Notify { public Ntf EventOccur; } class EventListen { private string name; public EventListen(string name) { this.name = name; } public void Happen(string message) { Console.WriteLine("{0} : {1}", name, message); } } class Program { static void Main(string[] args) { Notify n = new Notify(); EventListen l1 = new EventListen("L1"); EventListen l2 = new EventListen("L2"); EventListen l3 = new EventListen("L3"); //+= 연산자로 체인 만들기 n.EventOccur += l1.Happen; n.EventOccur += l2.Happen; n.EventOccur += l3.Happen; n.EventOccur("first"); Console.WriteLine(); //-= 연산자로 체인 제거 n.EventOccur -= l2.Happen; n.EventOccur("second"); Console.WriteLine(); //+와 =로 체인 만들기 n.EventOccur = new Ntf(l2.Happen) + new Ntf(l3.Happen); n.EventOccur("third"); Console.WriteLine(); Ntf n1 = new Ntf(l1.Happen); Ntf n2 = new Ntf(l2.Happen); //Combine 메소드로 체인 만들기 n.EventOccur = (Ntf)Delegate.Combine(n1, n2); n.EventOccur("fourth"); Console.WriteLine(); //Remove 메소드로 체인 끊기 n.EventOccur = (Ntf)Delegate.Remove(n.EventOccur, n2); n.EventOccur("fifth"); Console.WriteLine(); } } | 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 | delegate int Comp(int a, int b); class Program { static void Bubble(int[] Data, Comp c) { int i = 0, j = 0, t = 0; for(i = 0; i < Data.Length-1;++i) for(j=0;j<Data.Length-(i+1);++j) if(c(Data[j],Data[j+1])>0) { t = Data[j + 1]; Data[j + 1] = Data[j]; Data[j] = t; } } static void Main(string[] args) { int[] ar = { 3, 5, 4, 1, 9 }; Bubble(ar, delegate (int a, int b) { if (a > b) return 1; else if (a == b) return 0; else return -1; });//익명 메소드. 델리게이트 호출에 메소드를 넣는다. foreach (int i in ar) Console.WriteLine(i); } } | cs |
Comments