浅析C#中的委托:
首先通过一个例子来阐述delegate的用法。
using System;
class MyDelegate
{
static void chEat(string food)
{
Console.WriteLine("葱花吃"+food);
}
static void Main()
{
chEat("西瓜");
}
}
下面声明一个delegate实例,然后调用。
using System;
delegate void EatDelegate(string food);//此处签名应该与chEat方法签名保持一致
class MyDelegate
{
static void chEat(string food)
{
Console.WriteLine("葱花吃"+food);
}
static void Main()
{
EatDelegate ch=new EatDelegate(chEat);
ch("西瓜");
}
}
输出结果是:
葱花吃西瓜
样样吃西瓜
大蒜吃西瓜
但是:声明三次方法过于麻烦,用"委托链"解决。代码如下:
using System;
delegate void EatDelegate(string food);
class MyDelegate
{
static void chEat(string food)
{
Console.WriteLine("葱花吃"+food);
}
static void yyEat(string food)
{
Console.WriteLine("样样吃"+food);
}
static void dsEat(string food)
{
Console.WriteLine("大蒜吃"+food);
}
static void Main()
{
EatDelegate ch=new EatDelegate(chEat);
EatDelegate yy=new EatDelegate(yyEat);
EatDelegate ds=new EatDelegate(dsEat);
EatDelegate EatChain; //使用委托链解决
eatChain=ch+yy+ds;
eatChain("西瓜");
}
}
为了让程序富有逻辑性,同时为了说明C#2.0的匿名方法,请看如下代码:
using System;
delegate void EatDelegate(string food);
class MyDelegate
{
static void chEat(string food)
{
Console.WriteLine("葱花吃"+food);
}
static void yyEat(string food)
{
Console.WriteLine("样样吃"+food);
}
static void dsEat(string food)
{
Console.WriteLine("大蒜吃"+food);
}
static void Main()
{
EatDelegate ch=new EatDelegate(chEat);
EatDelegate yy=new EatDelegate(yyEat);
EatDelegate ds=new EatDelegate(dsEat);
EatDelegate eatChain;
Console.WriteLine("葱花,样样,大蒜在聊天");
eatChain=ch+yy+ds;
eatChain("西瓜");
Console.WriteLine("葱花出去接她男朋友的电话");
eatChain-=ch; //C sharp重载了-= +=
eatChain("葡萄");
Console.WriteLine("葱花约会回来了");
eatChain+=ch;
eatChain("芒果");
}
}
请注意:上面chEat(),yyEat(),dsEat()过于繁琐,可以用C# 2.0匿名方法解决:
using System;
delegate void EatDelegate(string food);
class MyDelegate
{
static void Main()
{
EatDelegate eatChain=null;
eatChain+=delegate(string food){Console.WriteLine("葱花吃"+food);};
eatChain+=delegate(string food){Console.WriteLine("样样吃"+food);};
eatChain+=delegate(string food){Console.WriteLine("大蒜吃"+food);};
eatChain("西瓜");
}
}
输出结果:
葱花吃西瓜
样样吃西瓜
大蒜吃西瓜
由此可以得出委托的特点:
(1)委托是类型安全的。
(2)委托允许将方法作为参数传递。
(3)委托可以定位回调方法。
(4)委托可以定义委托链。
首先通过一个例子来阐述delegate的用法。
using System;
class MyDelegate
{
static void chEat(string food)
{
Console.WriteLine("葱花吃"+food);
}
static void Main()
{
chEat("西瓜");
}
}
下面声明一个delegate实例,然后调用。
using System;
delegate void EatDelegate(string food);//此处签名应该与chEat方法签名保持一致
class MyDelegate
{
static void chEat(string food)
{
Console.WriteLine("葱花吃"+food);
}
static void Main()
{
EatDelegate ch=new EatDelegate(chEat);
ch("西瓜");
}
}
输出结果是:
葱花吃西瓜
样样吃西瓜
大蒜吃西瓜
但是:声明三次方法过于麻烦,用"委托链"解决。代码如下:
using System;
delegate void EatDelegate(string food);
class MyDelegate
{
static void chEat(string food)
{
Console.WriteLine("葱花吃"+food);
}
static void yyEat(string food)
{
Console.WriteLine("样样吃"+food);
}
static void dsEat(string food)
{
Console.WriteLine("大蒜吃"+food);
}
static void Main()
{
EatDelegate ch=new EatDelegate(chEat);
EatDelegate yy=new EatDelegate(yyEat);
EatDelegate ds=new EatDelegate(dsEat);
EatDelegate EatChain; //使用委托链解决
eatChain=ch+yy+ds;
eatChain("西瓜");
}
}
为了让程序富有逻辑性,同时为了说明C#2.0的匿名方法,请看如下代码:
using System;
delegate void EatDelegate(string food);
class MyDelegate
{
static void chEat(string food)
{
Console.WriteLine("葱花吃"+food);
}
static void yyEat(string food)
{
Console.WriteLine("样样吃"+food);
}
static void dsEat(string food)
{
Console.WriteLine("大蒜吃"+food);
}
static void Main()
{
EatDelegate ch=new EatDelegate(chEat);
EatDelegate yy=new EatDelegate(yyEat);
EatDelegate ds=new EatDelegate(dsEat);
EatDelegate eatChain;
Console.WriteLine("葱花,样样,大蒜在聊天");
eatChain=ch+yy+ds;
eatChain("西瓜");
Console.WriteLine("葱花出去接她男朋友的电话");
eatChain-=ch; //C sharp重载了-= +=
eatChain("葡萄");
Console.WriteLine("葱花约会回来了");
eatChain+=ch;
eatChain("芒果");
}
}
请注意:上面chEat(),yyEat(),dsEat()过于繁琐,可以用C# 2.0匿名方法解决:
using System;
delegate void EatDelegate(string food);
class MyDelegate
{
static void Main()
{
EatDelegate eatChain=null;
eatChain+=delegate(string food){Console.WriteLine("葱花吃"+food);};
eatChain+=delegate(string food){Console.WriteLine("样样吃"+food);};
eatChain+=delegate(string food){Console.WriteLine("大蒜吃"+food);};
eatChain("西瓜");
}
}
输出结果:
葱花吃西瓜
样样吃西瓜
大蒜吃西瓜
由此可以得出委托的特点:
(1)委托是类型安全的。
(2)委托允许将方法作为参数传递。
(3)委托可以定位回调方法。
(4)委托可以定义委托链。