举个委托的小例子?(c#)

来源:百度知道 编辑:UC知道 时间:2024/05/09 04:17:20

class Myclass
{
public event EventHandler Myevent;

public Myclass()
{
Myevent += new EventHandler(Myclass_Myevent);
}
//触发事件函数
public void fun()
{
Myevent(null ,new EventArgs ());
}

void Myclass_Myevent(object sender, EventArgs e)
{
//some code
}
}
static void main()
{
Myclass a = new Myclass();
a.fun();//这种就触发了事件 myevent
}

事件是一种特殊的委托
public event EventHandler Myevent;
EventHandler 是系统以经定义好的一种委托类型

//建立一个委托
public delegate void 媒婆();
//给自己找老公
class 自己
{
void 找老公()
{
console.writeline("我要找一个老公");
}

}
class Test
{
static void Main(){
自己 ziji=new 自己();
媒婆 meipo=new 媒婆(ziji.找老公());
meipo();
}

}

呵呵,我才做的一个.
委托就看作C++里的指针.
using System;
using System.Collections.Generic;
using System.Linq;
usi