C# Lambda
匿名函数
delegate void StudentDelegate(string name, int age); class Program { static void Main(string[] args) { } public void show() { DateTime dateTime = DateTime.Now; //历史 //版本1 { StudentDelegate student = new StudentDelegate(Student); } //版本2(这样写的话可以访问局部变量) { StudentDelegate student = new StudentDelegate(delegate (string name, int age) { Console.WriteLine(dateTime); Console.WriteLine($"我的名字是{name},年龄是{age}"); }); student("名字", 1); } //版本3(=>念成goes to) { StudentDelegate student = new StudentDelegate((string name, int age)=> { Console.WriteLine(dateTime); Console.WriteLine($"我的名字是{name},年龄是{age}"); }); student("名字", 1); } { Action action = () => Console.WriteLine("无返回值,无参数"); Action<DateTime> action1 = d => { Console.WriteLine(d + "带一个参数"); }; Func<DateTime> func = () => { return DateTime.Now; };//带一个返回值 DateTime dateTime1 = func();//调用Lambda获取值 Func<DateTime> func1 = () => DateTime.Now; } } public static void Student(string name ,int age ) { Console.WriteLine($"我的名字是{name},年龄是{age}"); } } }