永发信息网

C#, 举个简单的能运行的 委托例子.谢谢

答案:4  悬赏:60  手机版
解决时间 2021-03-16 14:29
  • 提问者网友:我没有何以琛的痴心不悔
  • 2021-03-15 14:17
C#, 举个简单的能运行的 委托例子.谢谢
最佳答案
  • 五星知识达人网友:千夜
  • 2021-03-15 14:54
public delegate void GreetingDelegate(string name);
class DelegateTest
{
public void ChinaGreeting(string name)
{
MessageBox.Show(name+",早上好!");
}
public void EnglishGreeting(string name)
{
MessageBox.Show(name+",Good Morning!");
}
public static void Greeting(string name,GreetingDelegate makeGreeting)
{
makeGreeting(name);
}

public string GetStr()
{
ServiceTest.ServiceTestClient clent=new ServiceTest.ServiceTestClient();
string s=clent.GetData(23);
return s;
}

public void TestDelegate()
{

GreetingManage gm = new GreetingManage();
gm.Add_GreetingDelegate(ChinaGreeting);
gm.Add_GreetingDelegate(EnglishGreeting);
// gm.greetingDelegate += EnglishGreeting;
gm.GreetingPeople("gm");
}

}
全部回答
  • 1楼网友:杯酒困英雄
  • 2021-03-15 18:11
Hello World!
你是要多简单的?源代码?
  • 2楼网友:夜余生
  • 2021-03-15 17:15
先定义一个委托 public delegate void DoTestAction(object o);
然后定义一个和该委托参数和返回值一致的方法
public void TestAction(object o) {
//doaction here.
}
申明
public DoTestAction testAction;
赋值并调用
public DoTestAction testAction;
public void DoIt() {
testAction += new DoTestAction(TestAction);
object o = new object();
testAction(o);
}
  • 3楼网友:我住北渡口
  • 2021-03-15 16:33
protected override void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btZH.Click += new System.EventHandler(this.btZH_Click);
this.btEN.Click += new System.EventHandler(this.btEN_Click);
this.btVN.Click += new System.EventHandler(this.btVN_Click);
this.btInquire.Click += new System.EventHandler(this.btInquire_Click);
//this.btDetails.Click += new System.EventHandler(this.btLookCertDetails_Click);
//this.GridView1.RowCreated += new GridViewRowEventHandler(this.GridView1_RowCreated);
this.Load += new System.EventHandler(this.Page_Load);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯