람다식 예제
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 | using System; using System.Linq; namespace EX10_LamdaExpression { delegate int Operator(int a, int b); delegate void OperatorCB(int a, int b); class MainClass { public static Operator calculate; public static OperatorCB calculateCB; public static void Main (string[] args) { // Lamda Expression calculate = (int a, int b) => a + b; Console.WriteLine ("{0} + {1} = {2}", 83, 34, calculate (83, 34)); calculate = (int a, int b) => a - b; Console.WriteLine ("{0} + {1} = {2}", 83, 34, calculate (83, 34)); calculate = (int a, int b) => a * b; Console.WriteLine ("{0} + {1} = {2}", 83, 34, calculate (83, 34)); calculate = (int a, int b) => a / b; Console.WriteLine ("{0} + {1} = {2}", 83, 34, calculate (83, 34)); // Lamda Expression Statement calculateCB = (int a, int b) => { int result = a % b; Console.WriteLine ("{0} % {1} = {2}", a, b, result); }; calculateCB(83, 34); // Lamda Expression in LINQ int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0, 13, 15 }; int oddNumberCount = numbers.Where (n => n % 2 != 0).Count(); Console.WriteLine (oddNumberCount); } } } | cs |
'Programming > C#' 카테고리의 다른 글
[C# 프로그래밍] 13장 애트리뷰트 (0) | 2016.01.11 |
---|---|
[C# 프로그래밍] 12장 리플렉션 (0) | 2016.01.11 |
[C# 프로그래밍] 10장 LINQ (0) | 2016.01.11 |
[C# 프로그래밍] 9장 델리게이트와 이벤트 (0) | 2016.01.11 |
[C# 프로그래밍] 8장 반복기와 양보문 (0) | 2016.01.11 |