c# 委託和事件執行個體學習

來源:互聯網
上載者:User

Common.cs: 複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateAndEvent.App_Code
{
public class Common
{
//定義全域變數.
public static string txt = "";
#region 定義方法
public string HelloCSharp(string name)
{
txt += "hello " + name;//這樣做是為了看到委託可以執行多個方法.
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region 定義委託
//定義委託和定義方法類似,區別是加個delegate.去掉方法體,唯寫方法簽名.
public delegate string SayHi(string name);
//委託可以像普通變數一樣使用.區別在於可以把多個方法賦給委託.
public SayHi dlgt1, dlgt2;
//使用委託
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region 事件
//聲明事件
public event SayHi hiEvent;
//觸發事件
public void causeEvent()
{
hiEvent += HelloCSharp;
hiEvent += HiCSharp;
if (hiEvent != null)
{
hiEvent("crane");
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateAndEvent.App_Code
{
public class Common
{
//定義全域變數.
public static string txt = "";
#region 定義方法
public string HelloCSharp(string name)
{
txt += "hello " + name;//這樣做是為了看到委託可以執行多個方法.
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region 定義委託
//定義委託和定義方法類似,區別是加個delegate.去掉方法體,唯寫方法簽名.
public delegate string SayHi(string name);
//委託可以像普通變數一樣使用.區別在於可以把多個方法賦給委託.
public SayHi dlgt1, dlgt2;
//使用委託
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region 事件
//聲明事件
public event SayHi hiEvent;
//觸發事件
public void causeEvent()
{
hiEvent += HelloCSharp;
hiEvent += HiCSharp;
if (hiEvent != null)
{
hiEvent("crane");
}
}
#endregion
}
}

MainFrm.cs: 複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DelegateAndEvent.App_Code;
namespace DelegateAndEvent
{
public partial class MainFrm : Form
{
Common common = new Common();
public MainFrm()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
//this.lblShow.Text += common.HelloCSharp("tree");
//測試委託
common.dlgt1 = common.HelloCSharp;//唯寫方法簽名,不加()
common.dlgt1 += common.HiCSharp;//雖然兩個方法都調用了,但是傳回值只返回最後一次調用的值.
//this.lblShow.Text += common.dlgt1("tree");//使用委託就像使用方法一樣.
//this.lblShow.Text = Common.txt;
//用委託做參數
//common.useDelegate("tree", common.dlgt1);
//this.lblShow.Text = Common.txt;
//事件
/*這裡的問題是不能用common.hiEvent();這樣引用.
原因是需要在這個類裡定義一個事件變數.
*/
common.causeEvent();
this.lblShow.Text = Common.txt;
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.