람다식 예제

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}"8334, calculate (8334));
 
            calculate = (int a, int b) => a - b;
            Console.WriteLine ("{0} + {1} = {2}"8334, calculate (8334));
 
            calculate = (int a, int b) => a * b;
            Console.WriteLine ("{0} + {1} = {2}"8334, calculate (8334));
 
            calculate = (int a, int b) => a / b;
            Console.WriteLine ("{0} + {1} = {2}"8334, calculate (8334));
 
            // Lamda Expression Statement
            calculateCB = (int a, int b) => {
                int result = a % b;
                Console.WriteLine ("{0} % {1} = {2}", a, b, result);
            };
            calculateCB(8334);
 
            // Lamda Expression in LINQ
            int[] numbers = { 54139867201315 };
            int oddNumberCount = numbers.Where (n => n % 2 != 0).Count();
            Console.WriteLine (oddNumberCount);
        }
    }
}
cs


+ Recent posts