c# in deep 之Lambda表达式
從很多方面,Lambda表達式都可以看作是C# 2的匿名方法的一種演變。匿名方法能做的幾乎一切事情都可以用Lambda表達式來完成,而且其更簡潔、易讀。下面是一個簡單例子。
class Film
??? {
??????? public string Name { get; set; }
??????? public int Year { get; set; }
??? }
??? class Program
??? {
??????? static void Main()
??????? {
??????????? var films = new List<Film>
??????????? {
??????????????? new Film { Name = "Jaws", Year = 1975 },
??????????????? new Film { Name = "Sing in the Rain", Year = 1952 },
??????????????? new Film { Name = "Some Like it Hot", Year = 1959 },
??????????????? new Film { Name = "The Wizard of oz", Year = 1939 },
??????????????? new Film { Name = "It's a Wonderful Life", Year = 1946 },
??????????????? new Film { Name = "American Beauty", Year = 1999 },
??????????????? new Film { Name = "High Fidelity", Year = 2000 },
??????????????? new Film { Name = "The Usual Suspects", Year = 1995 }
??????????? };
??????????? Action<Film> print = //創建可重用的列表打印委托
??????????????? film => Console.WriteLine("Name={0},Year={1}", film.Name, film.Year);
??????????? films.ForEach(print); //打印原始列表
??????????? Console.WriteLine("---------------------------------------");
??????????? films.FindAll(film => film.Year < 1960).ForEach(print);
??????????? Console.WriteLine("---------------------------------------");
??????????? films.Sort((f1, f2) => f1.Name.CompareTo(f2.Name));
??????????? films.ForEach(print);
??????????? Console.WriteLine("---------------------------------------");
??????????? Console.ReadKey();
??????? }
??? }
轉載于:https://www.cnblogs.com/xuekai-to-sharp/p/3359680.html
總結
以上是生活随笔為你收集整理的c# in deep 之Lambda表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 6-5
- 下一篇: modelsim仿真