c# delegate
![[C#] 람다 표현식 (Lambda Expression)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcCvjMS%2FbtqDvXZOnHf%2FZTdbRHGVRWP0KKlYJIdP61%2Fimg.png)
[C#] 람다 표현식 (Lambda Expression)
1. 핵심 정리 using System; class Program { public static void foo(Func f) { int s = f(1, 2); Console.WriteLine(s); } static void Main(string[] args) { foo( ? ); } } Main에서 foo함수의 인자로 메소드를 받는데 메소드의 이름이 아닌 구현부를 넣을 수 있다. 이름을 제외한 인자~구현부까지 넣으면 되는데 형태는 아래와 같다. using System; class Program { public static void foo(Func f) { int s = f(1, 2); Console.WriteLine(s); } static void Main(string[] args) { foo(Add);..
![[C#] Action, Func](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FblaC2F%2FbtqDvhjeq3z%2FiFi7j3ou3XdMH7WX9NdPYK%2Fimg.png)
[C#] Action, Func
1. Action using System; class Program { public static void Foo1(int arg1) { } public static void Foo2(int arg1, int arg2) { } static void Main() { ? f1 = Foo1; ? f2 = Foo2; } } Main에서 Foo1, Foo2 메소드의 정보를 담고 싶다. 그렇다면 앞선 내용대로 delegate를 사용하면 된다. using System; delegate void Foo1(int arg1); delegate void Foo2(int arg1, int arg2); class Program { public static void Foo1(int arg1) { } public static voi..
![[C#] Delegate Chain](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbBSX9x%2FbtqDgye3GLg%2F3kWTqWu8GlHoQpbOKpemQk%2Fimg.png)
[C#] Delegate Chain
1. Delegate Combine using System; class Test { public static int Method1() { Console.WriteLine("Method1"); return 1; } public static int Method2() { Console.WriteLine("Method2"); return 2; } public static int Method3() { Console.WriteLine("Method3"); return 3; } public static int Method4() { Console.WriteLine("Method4"); return 4; } } delegate int FUNC(); class Program { public static void Main(..
![[C#] Delegate Example](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FS3bbY%2FbtqDc4Z9TfU%2FLBxOoUG8pk7alEQ7K83lgK%2Fimg.png)
[C#] Delegate Example
1. 예제 using System; class Program { public static void Swap(ref int a, ref int b) { int temp = a; a = b; b = temp; } public static void Sort(int[] arr) { int sz = arr.GetLength(0); for(int i = 0; i arr[j]) Swap(ref arr[i], ref arr[j]); } } } static void Main() { int[] x = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}; Sort(x); foreach(int n in x) ..
![[C#] Delegate (2)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FNJ2N7%2FbtqDbFzh7MT%2FinrXarSc9vrHOr5GfCK6Y0%2Fimg.png)
[C#] Delegate (2)
1. Delegate에 메소드를 등록하는 방법 using System; delegate void FUNC(int arg); class Test { public static void static_method(int arg) { } public void instance_method(int arg) { } } class Program { public static void static_method(int arg) { } public void instance_method(int arg) { } public static void Main() { FUNC f1 = Test.static_method; // FUNC f2 = Program.static_method; // ok FUNC f2 = static_method;..
![[C#] Delegate (1)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FZ0dps%2FbtqDdG31whB%2FGc4BKGejd1CVLkkfh2BJa0%2Fimg.png)
[C#] Delegate (1)
1. 핵심 정리 using System; delegate void FUNC(int arg); class Program { static void Main() { int n = 10; double d = 3.4; string s = "hello"; // ? f = foo; FUNC f = foo; } public static void foo(int arg) { Console.WriteLine($"foo : {arg}"); } } 정수형 데이터 타입을 담는 int 실수형 데이터 타입을 담는 double 문자열 데이터 타입을 담는 string 그렇다면 11번째 라인과 같이 함수를 담는 데이터 타입을 무엇일까? ① Delegate => 메소드(메소드의 호출 정보, 메소드 모양/주소)를 저장하는 타입 => ( ) ..