c# 委托和事件實例學習
更新時間:2009年01月30日 18:28:28 作者:
今天把委托和事件研究了一個,winForm環(huán)境下,一般的小例子都是字符界面,我為了運用一下,寫了winForm
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;//這樣做是為了看到委托可以執(zhí)行多個方法.
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region 定義委托
//定義委托和定義方法類似,區(qū)別是加個delegate.去掉方法體,只寫方法簽名.
public delegate string SayHi(string name);
//委托可以像普通變量一樣使用.區(qū)別在于可以把多個方法賦給委托.
public SayHi dlgt1, dlgt2;
//使用委托
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region 事件
//聲明事件
public event SayHi hiEvent;
//觸發(fā)事件
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;//這樣做是為了看到委托可以執(zhí)行多個方法.
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region 定義委托
//定義委托和定義方法類似,區(qū)別是加個delegate.去掉方法體,只寫方法簽名.
public delegate string SayHi(string name);
//委托可以像普通變量一樣使用.區(qū)別在于可以把多個方法賦給委托.
public SayHi dlgt1, dlgt2;
//使用委托
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region 事件
//聲明事件
public event SayHi hiEvent;
//觸發(fā)事件
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;
}
}
}
復制代碼 代碼如下:
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;//這樣做是為了看到委托可以執(zhí)行多個方法.
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region 定義委托
//定義委托和定義方法類似,區(qū)別是加個delegate.去掉方法體,只寫方法簽名.
public delegate string SayHi(string name);
//委托可以像普通變量一樣使用.區(qū)別在于可以把多個方法賦給委托.
public SayHi dlgt1, dlgt2;
//使用委托
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region 事件
//聲明事件
public event SayHi hiEvent;
//觸發(fā)事件
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;//這樣做是為了看到委托可以執(zhí)行多個方法.
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region 定義委托
//定義委托和定義方法類似,區(qū)別是加個delegate.去掉方法體,只寫方法簽名.
public delegate string SayHi(string name);
//委托可以像普通變量一樣使用.區(qū)別在于可以把多個方法賦給委托.
public SayHi dlgt1, dlgt2;
//使用委托
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region 事件
//聲明事件
public event SayHi hiEvent;
//觸發(fā)事件
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;
}
}
}
相關文章
Unity的AssetPostprocessor?Model動畫函數使用案例深究
這篇文章主要介紹了Unity的AssetPostprocessor?Model動畫函數使用案例的深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08C#拼接SQL語句 用ROW_NUMBER實現的高效分頁排序
C#拼接SQL語句,SQL Server 2005+,多行多列大數據量情況下,使用ROW_NUMBER實現的高效分頁排序2012-05-05c#中Empty()和DefalutIfEmpty()用法分析
這篇文章主要介紹了c#中Empty()和DefalutIfEmpty()用法,以實例形式分析了針對不同情況下Empty()和DefalutIfEmpty()用法區(qū)別,需要的朋友可以參考下2014-11-11