标签:view hid alt 操作符 code ring name 多个 logs
1 delegate 返回值 委托名(参数列表); 2 eg: 3 public delegate void SayHelloDelegate(string name);
1 public delegate void SayHelloDelegate(string name);//声明委托 2 public viod SayHelloEnglish(string name)//声明方法 3 { 4 Console.WriteLine("Hello,{0}",name); 5 } 6 SayHelloDelegate s=SayHelloEnglish;//把方法赋予委托对象
委托实例化的几种形式:
1 1.使用new关键字 2 SayHelloDelegate s=new SayHelloDelegate(SayHelloEnglish); 3 2.直接赋值 4 SayHelloDelegate s=SayHelloEnglish; 5 3.使用匿名方法 6 SayHelloDelegate s=delegate(string name) 7 { 8 Console.WriteLine("Hello,{0}",name); 9 } 10 4.使用Lambda表达式,例如: 11 SayHelloDelegate s=name=> 12 { 13 Console.WriteLine("Hello,{0}",name); 14 }
1 委托实例化后,就可以像调用方法一样调用委托。eg: 2 s("John");
1 public delegate void SayHelloDelegate(string name); 2 public void SayHelloEnglish(string name) 3 { 4 Console.WriteLine("Hello,{0}",name); 5 } 6 public void SayHelloChinese(string name); 7 { 8 Console.WriteLine("你好,{0}",name); 9 } 10 SayHelloDelegate s1=new SayHelloDelegate(SayHelloEnglish); 11 SayHelloDelegate s2=SayHelloChinese; 12 SayHelloDelegate s3=s1+s2;//操作符两边只能是委托对象
1 public delegate void SayHelloDelegate(string name); 2 public void SayHelloEnglish(string name) 3 { 4 Console.WriteLine("Hello,{0}",name); 5 } 6 public void SayHelloChinese(string name); 7 { 8 Console.WriteLine("你好,{0}",name); 9 } 10 SayHelloDelegate s=new SayHelloDelegate(SayHelloEnglish); 11 SayHelloDelegate s+=SayHelloChinese;//右边的操作对象是方法签名
标签:view hid alt 操作符 code ring name 多个 logs
原文地址:http://www.cnblogs.com/yuelien/p/6661462.html