C#中的事件与委托是什么啊?

qdujunjie 2006-12-21 09:58:53
C#中的事件与委托是什么啊?搞不太懂,大家能给讲一下吗?或者举个例子.谢谢!
...全文
741 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jishuzhichi 2006-12-28
  • 打赏
  • 举报
回复
我写了一个简单的委托例子
using System;

namespace consign
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>

//定义委托
public delegate double text(int x,int y);
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
text t1 = new text(text1);
Console.Write("输入两个值");
int v1 = Int32.Parse(Console.ReadLine());
int v2 = Int32.Parse(Console.ReadLine());

double res = t1(v1,v2);
Console.WriteLine("结果 :"+res);
Console.ReadLine();

}

static double text1(int e,int r)
{
return e*r;
}

}
}
个人感觉就是一个方法不能确定他的参数等需要的时候就定义一个委托,然后让这个委托去指向一个方法。
byhum 2006-12-21
  • 打赏
  • 举报
回复
委托是把一个方法做为另一个方法的参数来传递,事件是一种特殊的委托
dlzhangln 2006-12-21
  • 打赏
  • 举报
回复
看MSDN
java_mp5 2006-12-21
  • 打赏
  • 举报
回复
我的理解是这样:
1:定义一个委托.
2:声明一个这个委托类型的事件
3:定义一个和这个委托参数一样的函数

当激活这个事件的时候,这个事件就会去找这个这个委托.然后通过这个委托去调用写好的那个函数.执行了一个功能.

这是我对这个 机制的个人理解 不知道对不对 看有人帮助指正一下.
jcyluck 2006-12-21
  • 打赏
  • 举报
回复
正解
popunit 2006-12-21
  • 打赏
  • 举报
回复
/*事件*/
public delegate void TimeEventHandler(string s);//委托声明
class MyTime
{
public event TimeEventHandler Timer;//声明事件
public void OnTimer(string s)
{
if (null != Timer)
{
Timer(s);//引发事件
}
}
}

class ProcessTime
{
//事件处理
public void GenerateTime(string s)
{
Console.WriteLine("Hello {0}! The time is {1} now", s, DateTime.Now);
}
}
class TestTime
{
public static void Main()
{
ProcessTime p = new ProcessTime();
MyTime t = new MyTime();
t.Timer+=new TimeEventHandler(p.GenerateTime);//把事件与事件处理联系起来
t.OnTimer("Peter");
Console.Read();
}
}




/*委托*/
delegate void TimeDelegate(string s);//委托声明
class MyTime
{
public static void HelloTime(string s)
{
Console.WriteLine("Hello {0}! The time is {1} now", s, DateTime.Now);
}

public static void GoodbyeTime(string s)
{
Console.WriteLine("Goodbye {0}! The time is {1} now", s, DateTime.Now);
}

public void SayTime(string s)
{
Console.WriteLine("{0}! The time is {1} now", s, DateTime.Now);
}
}

class TestDelegate
{
public static void Main()
{
//委托实例化,创建委托实例a,并封装静态方法HelloTime
TimeDelegate a = new TimeDelegate(MyTime.HelloTime);
Console.WriteLine("Invoking delegate a:");
//委托调用,相当于调用方法MyTime.Hello("A")
a("A");
TimeDelegate b = new TimeDelegate(MyTime.GoodbyeTime);
Console.WriteLine("Invoking delegate b:");
b("B");
//委托实例c封装了两个方法HelloTime和GoodbyeTime
TimeDelegate c = a + b;
Console.WriteLine("Invoking delegate c:");
c("C");
c -= a;
Console.WriteLine("Invoking delegate c:");
c("C");
MyTime t = new MyTime();
TimeDelegate d = new TimeDelegate(t.SayTime);
Console.WriteLine("Invoking delegate d:");
d("D");
Console.Read();
}
}
BellStar 2006-12-21
  • 打赏
  • 举报
回复
事件的本质就是函数指针。
你可先声明一个委托A,然后再声明一个由委托A来实现的事件B。然后你在你的类的函数里加入事件B(当你执行这个函数时就会触发事件B)。触发事件B后要执行的程序由你实例化委托A时指向。
click是界面的一个事件,click触发后的执行的程序由委托EventHandler去指向。
ex:
this.btn1.click += new EventHandler(btn1_click);
public void btn1_click()
{... }
namhyuk 2006-12-21
  • 打赏
  • 举报
回复
事件机制就是利用委托实现的。
不用被委托或事件这两个名词儿迷惑住。
简单点,把它想成函数指针就行了。至少开始时。

当某件事发生时我想执行一段代码,但预先我不知道要执行哪个函数。其实说白了也就这把戏嘛。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧