// Declare delegate -- defines required signature:
delegate void SampleDelegate(string message);//声明一委托类型
class MainClass
{
// Regular method that matches signature:
static void SampleDelegateMethod(string message)//一静态方法
{
Console.WriteLine(message);
}
static void Main()
{
// Instantiate delegate with named method:用一个已经声明的方法初始化一委托对象
SampleDelegate d1 = SampleDelegateMethod;//声明一委托对象并为之挂接一方法
// Instantiate delegate with anonymous method用一个匿名方法来初始化一委托对象
SampleDelegate d2 = delegate(string message)//
{
Console.WriteLine(message);
};
d1("Hello");output:Hello
d2("World,http://www.dc9.cn/");output:World,http://www.dc9.cn/
}
}
下面的是一事件(特殊类型的委托)
public Form1()
{
InitializeComponent();
this.Click += this.MultiHandler;//窗体的单击事件挂接下面的一事件
}
private void MultiHandler(object sender, System.EventArgs e)
{
MessageBox.Show(((MouseEventArgs)e).Location.ToString());
}
// Create a delegate instance
delegate void Del(int x);
// Instantiate the delegate using an anonymous method
Del d = delegate(int k) { /* ... */ };等价于
Del d =delegate(int k)
{ 语句 }
Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };
delegate void Printer(string s);//又一索引(其参数为string类型)
Printer p = delegate(string j)//匿名方法:即P所关联的方法(匿名)
{
System.Console.WriteLine(j);
};
p("The delegate using the anonymous method is called.");
delegate string Printer(string s);
private void button1_Click(object sender, EventArgs e)
{
Printer yuanl =delegate(string j)
{
return (j)+"烦死了";
};
Console.WriteLine( yuanl("The delegate using the anonymous method is called.") );
}
delegate void Printer(string s);
private void button1_Click(object sender, EventArgs e)
{
Printer p = new Printer(Form1.DoWork);
p("http://www.dc9.cn/");
}
static void DoWork(string k)
{
System.Console.WriteLine(k);
}
delegate string Printer(string s);
private void button1_Click(object sender, EventArgs e)
{
Printer yuanl = j => j+"烦死了!!!";
Console.WriteLine(yuanl("The delegate using the anonymous method is called."));
}
public Form1()
{
InitializeComponent();
this.Click += (s, e) => { MessageBox.Show(((MouseEventArgs)e).Location.ToString());};
}
注意(s,e)的含义:S事件的发送者,e承载传递的数据
//下面的代码是一典型的委托应用
using System;
delegate string ConvertMethod(string inString);
public class DelegateExample
{
public static void Main()
{
ConvertMethod convertMeth = UppercaseString;
string name = "Dakota";
Console.WriteLine(convertMeth(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
using System;
public class GenericFunc
{
public static void Main()
{
Func<string, string> convertMethod = UppercaseString;
string name = "Dakota";
Console.WriteLine(convertMethod(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
delegate bool TestFunc(string fruit);
private void button1_Click(object sender, EventArgs e)
{
List<string> fruits =new List<string> { "apple", "http://www.dc9.cn", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
TestFunc f = new TestFunc(DoWork);
Func<string, bool> f2 = DoWork;
IEnumerable<string> query = fruits.Where(f2);//典型委托
foreach (string fruit in query)
Console.WriteLine(fruit);
}
private static bool DoWork(string k)
{
return k.Length < 6;
}
delegate bool TestFunc(string fruit);
private void button1_Click(object sender, EventArgs e)
{
List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "http://www.dc9.cn" };
TestFunc f = DoWork;
Func<string, bool> f2 =k=> k.Length < 6;//lambda表达式
IEnumerable<string> query = fruits.Where(f2);
foreach (string fruit in query)
Console.WriteLine(fruit);
}
private static bool DoWork(string k)
{
return k.Length < 6;
}
private void button1_Click(object sender, EventArgs e)
{
List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "http://www.dc9.cn" };
IEnumerable<string> query = fruits.Where(
delegate(string k){
return k.Length < 6;
}
);//匿名函数
foreach (string fruit in query)
Console.WriteLine(fruit);
}
private void button1_Click(object sender, EventArgs e)
{
List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "http://www.dc9.cn" };
IEnumerable<string> query = fruits.Where(k=>k.Length<6);//lambda表达式
foreach (string fruit in query)
Console.WriteLine(fruit);
}
public delegate string yuanl(string a, string b);
string yuan ="yuanl";
public static class LambdaTest
{
public static string yuananl(this string a, string y, yuanl yl)
{
return yl(a, y);
}
}
Console.WriteLine(yuan.yuananl(an ,(yuan,an)=>yuan+"love"+an));