亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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;
}
}
}

相關文章

  • Unity的AssetPostprocessor?Model動畫函數使用案例深究

    Unity的AssetPostprocessor?Model動畫函數使用案例深究

    這篇文章主要介紹了Unity的AssetPostprocessor?Model動畫函數使用案例的深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • C#操作注冊表的方法詳解

    C#操作注冊表的方法詳解

    這篇文章主要介紹了C#操作注冊表的方法,結合實例形式較為詳細的分析了C#針對注冊表的創(chuàng)建、打開、讀取、寫入、修改、刪除等技巧,需要的朋友可以參考下
    2015-12-12
  • C#多線程處理多個隊列數據的方法

    C#多線程處理多個隊列數據的方法

    這篇文章主要介紹了C#多線程處理多個隊列數據的方法,涉及C#線程與隊列的相關操作技巧,需要的朋友可以參考下
    2015-07-07
  • c# 在windows服務中 使用定時器實例代碼

    c# 在windows服務中 使用定時器實例代碼

    這篇文章主要介紹了c# 在windows服務中 使用定時器實例代碼,有需要的朋友可以參考一下
    2013-12-12
  • C#拼接SQL語句 用ROW_NUMBER實現的高效分頁排序

    C#拼接SQL語句 用ROW_NUMBER實現的高效分頁排序

    C#拼接SQL語句,SQL Server 2005+,多行多列大數據量情況下,使用ROW_NUMBER實現的高效分頁排序
    2012-05-05
  • c#中Empty()和DefalutIfEmpty()用法分析

    c#中Empty()和DefalutIfEmpty()用法分析

    這篇文章主要介紹了c#中Empty()和DefalutIfEmpty()用法,以實例形式分析了針對不同情況下Empty()和DefalutIfEmpty()用法區(qū)別,需要的朋友可以參考下
    2014-11-11
  • C#中DateTimePicker默認值顯示為空的問題

    C#中DateTimePicker默認值顯示為空的問題

    這篇文章主要介紹了C#中DateTimePicker默認值顯示為空的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • C#使用Chart繪制曲線

    C#使用Chart繪制曲線

    這篇文章主要為大家詳細介紹了C#使用Chart繪制曲線,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#中使用UDP通信實例

    C#中使用UDP通信實例

    這篇文章主要介紹了C#中使用UDP通信實例,非常實用的技巧,需要的朋友可以參考下
    2014-08-08
  • C#自定義RSA加密解密及RSA簽名和驗證類實例

    C#自定義RSA加密解密及RSA簽名和驗證類實例

    這篇文章主要介紹了C#自定義RSA加密解密及RSA簽名和驗證類,實例分析了C#實現RSA加密解密及RSA簽名和驗證的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03

最新評論