using System;
namespace ConsoleApp1
{
/// <summary>
/// 委托(匿名函数、Lambda表达式)
/// </summary>
class Program
{
//定义委托
internal delegate void TestDelegate(string s);
static void Main(string[] args)
{
Func<string> f1 = () => "Jeff";
//var result= f1();
//.net2.0时期的匿名函数写法
Func<string> f2 = delegate () { return "Jeff2"; };
//var result = f2.Invoke();
//.net3.0以后的Lambda表达式写法
//Func<Int32, string> f3 = (Int32 i) => i.ToString();
Func<Int32, string> f3 = (i) => i.ToString();
var result = f3(100);
Func<Int32, string> f4 = delegate (Int32 i) { return i.ToString(); };
//var result = f4(1000);
//Console.WriteLine(result);
//Console.ReadKey();
//1.实例化委托
TestDelegate testDelegate1 = new TestDelegate(ConsoleStr);
testDelegate1("测试委托1");
//2.匿名委托(.NET2.0 +)
TestDelegate testDelegate2 = delegate (string s) { Console.WriteLine(s); };
testDelegate2.Invoke("测试委托2");
//3.Lambda表达式
TestDelegate testDelegate3 = (s) => { Console.WriteLine(s); };
testDelegate3("测试委托3");
}
static void ConsoleStr(string s)
{
Console.WriteLine(s);
}
}
}