[C#] 17일차 - 118. Predicate<T> 델리게이트
C#/C#200제2021. 2. 15. 18:52[C#] 17일차 - 118. Predicate<T> 델리게이트

Predicate는 Func나 Action과 같은 미리 정의된 델리게이트 형식입니다. 리턴 값이 반드시 bool이고 입력 파라미터가 하나입니다. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace A118_Predicate { class Program { static void Main(string[] args) { Predicate isEven = n => n % 2 == 0; Console.WriteLine(isEven(6)); Predicate isLowerCase = s => s.Equals(s.ToLower()); Consol..

[C#] 17일차 - 117. 람다식의 사용
C#/C#200제2021. 2. 15. 18:42[C#] 17일차 - 117. 람다식의 사용

람다식은 익명메소드를 간단하게 표현할 수 있는 방법. 1. 식 람다 연산자의 오른쪽에 식이 있는것을 식 람다라고함. 식 람다는 식의 결과를 반환하며 기본 형식은 아래와 같음 (input-parameters) => expression 2. 문 람다 문 람다는 다음과 같이 중괄호 안에 문장을 지정한다는 점을 제외하면 식 람다와 비슷하다. (input-parameters) => { statement; } 간단한 예제 : 리턴값이 없고 매개변수가 하나인 메소드를 문 람다로 작성. greet("World"); 라고 호출하면 "Hello World!" 라고 출력됨 Action greet = name => { string greeting = $"Hello {name}!"; Console.WriteLine(greeti..

[C#] 16일차 - 115. Func와 Action으로 델리게이트를 더 간단히 만들기
C#/C#200제2021. 2. 14. 13:42[C#] 16일차 - 115. Func와 Action으로 델리게이트를 더 간단히 만들기

Func 델리게이트는 결과를 반환하는 메소드를 참조하기위해 Action 델리게이트는 결과를 반환하지 않는 메소드를 참조하기위해 사용된다. 거의 모든 델리게이트를 Func와 Action으로 쓸 수 있다. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace A115_FuncAndAction { class Program { // delegate bool MemberTest(int x); Func를 사용하므로 필요없어짐 static void Main(string[] args) { var arr = new[] { 3, 34, 6, 34, 7,..

[C#] 16일차 - 114. 이름 없는 델리게이트
C#/C#200제2021. 2. 14. 13:36[C#] 16일차 - 114. 이름 없는 델리게이트

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace A114_AnonymousDelegate { class Program { delegate bool MemberTest(int x); static void Main(string[] args) { var arr = new[] { 3, 34, 6, 34, 7, 8, 24, 3, 675, 8, 23 }; // 델리게이트 메소드를 이름없이 인라인으로 직접 정의함. int n = Count(arr, delegate (int x) { return x % 2 == 0; }); Console.Wri..

[C#] 16일차 - 113. Delegate의 기본, 배열에서 홀수와 짝수 찾기
C#/C#200제2021. 2. 14. 13:25[C#] 16일차 - 113. Delegate의 기본, 배열에서 홀수와 짝수 찾기

델리게이트는 대리자라는 뜻으로, 메소드의 참조, 즉 C언어의 함수포인터와 같은 개념이다. delegate 리턴형식 이름(매개변수 목록); using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace A113_DelegateExample { class Program { delegate bool MemberTest(int a); static void Main(string[] args) { int[] arr = new int[] { 3, 5, 4, 2, 6, 4, 6, 8, 54, 23, 4, 6, 4 }; Console.WriteLine("짝수의..

image