대리자는 메소드의 참조 혹은 콜백으로서 명령어들을 코드로 만든 다음 바로 호출하는 방식보다 다른 위치의 명령어의 집합을 호출할 수 있도록 전달하는 강력한 개념이다.


C# 3.0 에서는 Lambda 식을 소개하고 있고 

C# 2.0 에서는 익명 메소드 문법을 대체한다. (익명 메소드는 사라진게 아니다)


이런 닷넷의 특징들은 메소드 내에서 동적으로 명령들을 수행하는데 있어, 굉장한 유연성을 제공한다.

이것이 바로 LINQ라는 통합 언어 징의 (Language Integrated Query API를 이용해서 컬렉션에 대해 프로그래밍을 한결 쉽게 해주는 기능이다.


람다 식은 문 람다와 식 람다 두가지를 포괄한다. 

식 트리의 대한 개념과 대리자 구현 그대로가 아닌 람다 식의 의미론적인 분석을 표현하는 부분은

Linq to SQL 과 Linq to XML과 같은 컨텍스트 내에서 식트리를 해석하는 라이브러리의 핵심적인 특징이다.


example. 1


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Program
    {
        public enum SortType
        {
            Ascending,
            Desending
        }
 
        static void Main(string[] args)
        {
 
            ComparisonHandler c = (int first, int second) => { return first > second; };
            Func<int,int,bool> f = c.Invoke;
 
 
            int[] items = new int[] { 1121354};
 
            //문 람다
            //var result = BubbleSort(items, (int first, int second) => { return first > second;});
 
            //식 람다
            var result = BubbleSort(items, (int first, int second) => first > second);
 
            //익명 메소드
            //var result = BubbleSort(items, delegate(int first, int seconde){return first > seconde;});
 
            //매개변수가 없는 익명 메소드
            //var result = BubbleSort(items, delegate { return Console.ReadLine() != ""; });
            
 
            foreach (var item in result)
            {
                Console.WriteLine(item.ToString());
            }
 
            Console.ReadLine();
        }
 
        public static bool GreaterThan(int first, int second)
        {
            return first > second;
        }
 
        public static bool AlphabeticalGreaterThan(int first, int second)
        {
            int comparison;
 
            comparison = (first.ToString().CompareTo(second.ToString()));
 
            return comparison > 0;
        }
 
        public delegate bool ComparisonHandler(int first, int seconde);
 
        static int[] BubbleSort(int[] items, Func<int,int,bool> comparisonHandler)
        {
            int i;
            int j;
            int temp;
 
            if (items != null)
            {
                for (i = items.Length - 1; i >= 0; i--)
                {
                    for (j = 1; j <= i; j++)
                    {
                        if (comparisonHandler(items[j-1], items[j]))
                        {
                            temp = items[j - 1];
                            items[j - 1= items[j];
                            items[j] = temp;
                        }
                    }
                }
            }
 
            return items;
        }
 
        static int[] BubbleSort(int[] items, SortType sortOder)
        {
            int i;
            int j;
            int temp;
 
            if(items != null)
            {
                for ( i = items.Length - 1; i >= 0; i--)
                {
                    for ( j = 1; j <= i; j++)
                    {
                        bool swap = false;
 
                        switch(sortOder)
                        {
                            case SortType.Ascending:
                                swap = items[j - 1> items[j];
                                break;
                            case SortType.Desending:
                                swap = items[j - 1< items[j];
                                break;
                        }
 
                        if(swap)
                        {
                            temp = items[j - 1];
                            items[j - 1= items[j];
                            items[j] = temp;
                        }
                    }
                }
            }
 
            return items;
        }
    }
cs


example 2.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 List<string> stList = new List<string>() { "a""b""c""f""d""g" };
 
            var selection = stList.Where((st, index) =>
            {
                var item = index.ToString();
 
                if (st == "a" && index == 0)
                    return true;
                else
                    return false;
            });
 
            foreach (var item in selection)
            {
                Console.WriteLine(item);
            }
 
cs


'.NET 개발 > C#' 카테고리의 다른 글

Webp C#  (0) 2016.09.23
C#의 발전 내용  (0) 2016.06.06
lambda expression  (0) 2016.06.05
공변성, 반공변성  (0) 2016.06.05
Xaml web browser application .xbap 캐시 지우기  (0) 2015.12.21
Posted by Hoya0415
,