public partial class 委托_wtDemo3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// this.Label1.Text = ChineseGreeting("张明");
//delegateGreeting1 delegate1;
//delegateGreeting1 delegate2;
//delegate1 = ChineseGreeting;
//delegate2 = EnglishGreeting;
//delegateGreeting1 delegate1;
//delegate1 = EnglishGreeting;
delegateGreeting1 delegate1 = new delegateGreeting1(EnglishGreeting);
delegate1 += ChineseGreeting;
this.Label1.Text = delegate1("李四");
}
//英国人打招呼
protected string EnglishGreeting(string name)
{
return "hello," + name;
}
//中国人打招呼
protected string ChineseGreeting(string name)
{
return "你好," + name;
}
protected string Greeting(string name, delegateGreeting1 delegateGreeint2)
{
return delegateGreeint2(name);
}
}
public delegate string delegateGreeting1(string name);
执行的结果为:你好,李四
使用多路广播需要用new去创建
protected void Page_Load(object sender, EventArgs e)
{
// this.Label1.Text = ChineseGreeting("张明");
//delegateGreeting1 delegate1;
//delegateGreeting1 delegate2;
//delegate1 = ChineseGreeting;
//delegate2 = EnglishGreeting;
//delegateGreeting1 delegate1;
//delegate1 = EnglishGreeting;
delegateGreeting1 delegate1 = new delegateGreeting1(EnglishGreeting);
delegate1 += new delegateGreeting1(ChineseGreeting);
this.Label1.Text = delegate1("李四");
}
原因到这里看看http://www.cnblogs.com/dudu837/archive/2009/08/31/1557286.html
希望有收获
嗯,我知道了,谢谢,但是这种有返回值的情况下用new也不行,多点委托所引用的多个方法都有返回值的情况下,只有最后被调用的方法才有返回值。
this.Label1.Text = delegate1("李四");
delegate1里先后调用了2个方法,第一个返回值是"Hello, 李四",第二个返回值是"你好,李四",因此delegate1的返回值是第二个方法的返回值"你好,李四",然后这个string被赋值给了Label1的Text属性。 难道你认为这两个函数的返回值string会自动连接起来作为delegate1的返回值么……