Home  >  Article  >  Backend Development  >  Explanation of delegation examples in .net 2.0

Explanation of delegation examples in .net 2.0

零下一度
零下一度Original
2017-06-24 09:43:131365browse

Since .net 2.0 introduced anonymous methods, the way to create delegates can be simplified.

Delegation in .net 2.0

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn