Home > Article > Backend Development > Explanation of delegation examples in .net 2.0
Since .net 2.0 introduced anonymous methods, the way to create delegates can be simplified.
With anonymous method, the example in the previous article can be simplified to:
1 namespace DelegateDemo 2 { 3 //声明委托 4 public delegate void MyDel(string arg1, string arg2); 5 6 class Program 7 { 8 static void Main(string[] args) 9 {10 //.net 2.0中的委托11 12 //创建委托,使用匿名方法13 MyDel myDel = delegate(string arg1, string arg2)14 {15 Console.WriteLine(string.Format("arg1:{0},arg2:{1}", arg1, arg2));16 };17 18 //调用委托19 myDel("aaa", "bbb"); 20 21 Console.ReadKey(); 22 } 23 } 24 }
As you can see, you no longer need to define types and methods separately, you only need to use inline syntax to implement them.
The above is the detailed content of Explanation of delegation examples in .net 2.0. For more information, please follow other related articles on the PHP Chinese website!