asp.net 委托用法 适合新手
asp.net 委托用法 适合新手
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SJLERP
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetPeople("Dom", EnglishMessage);
GetPeople("于海涛", ChineseMessage);
delegateMethod dm= EnglishMessage;
dm += ChineseMessage;
dm("王俊英");
//delegateMethod delegate1, delegate2;
//delegate1 = EnglishMessage;
//delegate2 = ChineseGreeting;
//GreetPeople("Dom", delegate1);
//GreetPeople("于海涛", delegate2);
//将多个方法绑定到同一个委托
//delegateMethod delegate1;
//delegate1 = EnglishMessage;
//delegate1 += ChineseGreeting;
//GreetPeople("于海涛", delegate1);
//通过委托直接调用方法
//delegateMethod delegate1;
//更多:http://www.52mvc.com
//delegate1 = EnglishMessage;
//delegate1 += ChineseGreeting;
//delegate1("于海涛");
//delegateMethod delegate1 = new delegateMethod(EnglishMessage);
//delegate1 += ChineseGreeting;
//delegate1("于海涛");
//取消对ChineseGreeting方法的绑定
//delegate1 -= ChineseGreeting;
//delegate1("Dom");
}
public delegate void delegateMethod(string name);
public void EnglishMessage(string name)
{
Response.Write("hi:" + name+"<br>");
}
public void ChineseMessage(string name)
{
Response.Write("嗨:" + name + "<br>");
}
public void GetPeople(string name, delegateMethod dm)
{
dm(name);
}
}
}
更多: http://www.52mvc.com/