namespace lambda { delegate void TestDelegate(string s); class Program { static void Main(string[] args) { //动态构建C# Lambda表达式例子1 var ints = new int[10] {10,2,3,4,5,2,34,54,4,40}; var newints = ints.Where(i => i > 20); foreach (var a in newints) { Console.WriteLine(a); } Array.ForEach<int>(ints, i => Console.WriteLine(i)); TestDelegate del = (s) => { string n = s + ":hello"; Console.WriteLine((n)); }; del("tom"); Action<string> ac = (s) => { Console.WriteLine(s+":hello");}; ac("action"); Func<string, string> fun = s => { return s + ":hello"; }; Console.WriteLine(fun("func")); Func<int, string, bool> myFunc = (x, y) => (x.ToString() == y); Console.WriteLine(myFunc(3, "3")); var q = from e in ints select new { test = e > 5 ? e : -1 }; foreach (var v in q) { Console.WriteLine(v.test); } Console.ReadKey(); } } }
时间: 2024-10-01 04:14:16