先引用MSDN上的一段话,以免误导大家:
“委托是一种定义方法签名的类型。当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联。您可以通过委托实例调用方法。
委托用于将方法作为参数传递给其他方法。事件处理程序就是通过委托调用的方法。您可以创建一个自定义方法,当发生特定事件时某个类(例如 Windows 控件)就可以调用您的方法。”
说实话,我看了很多关于委托的文章,也有非常好的,浅显易懂的,看的时候非常明白,可是回头就忘记了,因为我就没有用过它。今天再次和伙伴们一起讨论了这个东西,想要记录下来,还期望能不再忘记!
话说有一对小夫妻,男的不是气管炎,家庭是比较相敬如宾,蛮和谐的。一天丈夫委托妻子去买菜,说:“老婆,今天我们准备吃素食,你今天回来的时候在菜市场买些蔬菜”,老婆说“好的,不过我要是忘记了怎么办啊?”老公此时立马笑脸相迎说道:“你要是忘记了,那我就去买!”于是就有了下面的代码:
public class Husband
{
public delegate void BuyVegetableDelegate();
// You can definte delegate outside any class, but create instance the place where you want to use
public BuyVegetableDelegate BuyVegetableDel;//Create an instance of BuyVegetableDelegate
public void BuyVegetable()
{
Console.WriteLine("Tonight we eat vegetable");
if (BuyVegetableDel != null) //If have delegated somebody to buy vegetable
BuyVegetableDel(); // Performed delegate: Buy vegetable
else
Console.WriteLine("Husband buy vegetable");
}
}
那么妻子会买什么菜呢?老公没有说,不过一定是蔬菜的,好把,我想就买这些蔬菜吧:
public class Wife
{
public void BuyCabbage()
{
Console.WriteLine("Wife buy cabbage");
}
public void BuyPotato()
{
Console.WriteLine("Wife buy potatoes");
}
public void BuyTomato()
{
Console.WriteLine("Wife buy tomatoes");
}
}
既然这么定了,那就期待今晚的素食烛光晚餐吧
static void Main(string[] args)
{
Husband husband = new Husband();// Create an instance of Husband
Wife wife = new Wife();// Create an instance of Wife
husband.BuyVegetableDel += wife.BuyCabbage;
husband.BuyVegetableDel += wife.BuyPotato;
husband.BuyVegetableDel += wife.BuyTomato;
/*
husband.BuyVegetableDel = new Husband.BuyVegetableDelegate(wife.BuyCabbage) + new Husband.BuyVegetableDelegate(wife.BuyPotato) + new Husband.BuyVegetableDelegate(wife.BuyTomato);
// Here we can use "=" to add delegate
*/
/*
husband.BuyVegetableDel += wife.BuyCabbage;
husband.BuyVegetableDel += new Husband.BuyVegetableDelegate(wife.BuyPotato) + new Husband.BuyVegetableDelegate(wife.BuyTomato);
// Here we must use "+=", because you will hide wife.BuyCabbage if you use "="
*/
husband.BuyVegetable();
}
这就是一个完整的委托了,只是不带参数。如果想要带参数,那就委托妈妈吧
public delegate void BuyVegetableDelegate(String s, int i)
Mom mom = new Mom();
BuyVegetableDelegate momBuyVegetable;
momBuyVegetable = mom.BuyCabbage;
// Or do it like below
//BuyVegetableDelegate momBuyVegetable = new BuyVegetableDelegate(mom.BuyCabbage);
momBuyVegetable+=mom.BuyPotato;
momBuyVegetable+=mom.BuyTomato;
momBuyVegetable("Dear Mom", 2);
public class Mom
{
public void BuyCabbage(String s, int i)
{
Console.WriteLine(s + ", please buy " + i + " catty cabbage");
}
public void BuyPotato(String s, int i)
{
Console.WriteLine(s + ", please buy " + i + " catty potatoes");
}
public void BuyTomato(String s, int i)
{
Console.WriteLine(s + ", please buy " + i + " catty tomatoes");
}
}
另外再附上委托的特点:
-
委托类似于 C++ 函数指针,但它们是类型安全的。
-
委托允许将方法作为参数进行传递。
-
委托可用于定义回调方法。
-
委托可以链接在一起;例如,可以对一个事件调用多个方法。
-
方法不必与委托签名完全匹配。有关更多信息,请参见在委托中使用变体(C# 和 Visual Basic)。
-
C# 2.0 版引入了匿名方法的概念,此类方法允许将代码块作为参数传递,以代替单独定义的方法。C# 3.0 引入了 Lambda 表达式,利用它们可以更简练地编写内联代码块。匿名方法和 Lambda 表达式(在某些上下文中)都可编译为委托类型。这些功能统称为匿名函数。有关 lambda 表达式的更多信息,请参见匿名函数(C# 编程指南)。
-
可以做到低耦合
至于为什么让委托出现?我们何时才需要使用委托?如何使用它?这些问题期待大家的帮助!