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 | class Program { static void Main(string[] args) { //반공변성 Action<object> broadAction = (object data) => { Console.WriteLine(data); }; Action<string> narrowAction = broadAction; // 공변성 Func<string> narrowFunction = () => Console.ReadLine(); Func<object> broadFunction = narrowFunction; //반공변성과 공변성 조합 Func<object, string> func1 = (object data) => data.ToString(); Func<string, object> func2 = func1; var items = new string[] { "Moe", "Larry", "Curly" }; var actions = new List<Action>(); for (int i = 0; i < items.Count(); i++) { int j = i; Console.WriteLine(items[j]); actions.Add(() => { Console.WriteLine(items[j]); }); } //foreach (string item in items) //{ // actions.Add(() => { Console.WriteLine(item); }); //} foreach (Action action in actions) { action(); } } } | cs |
'.NET 개발 > C#' 카테고리의 다른 글
대리자와 람다 식 (0) | 2016.06.05 |
---|---|
lambda expression (0) | 2016.06.05 |
Xaml web browser application .xbap 캐시 지우기 (0) | 2015.12.21 |
WebSecurity 클래스 (0) | 2015.12.03 |
NamedPipeClientStream 에러 : System.UnauthorizedAccessException (0) | 2015.11.17 |