【发布时间】:2014-04-19 04:31:07
【问题描述】:
我有一个关于 C# 中的事件和委托的问题。我在许多代码中看到他们使用event 和delegate 关键字来创建事件触发器。让我们先跳过它,我比较关心的是事件触发的函数,或者更确切地说,这里调用的函数是作用域 sn-p。
public delegate void EventHandler();
class Program
{
//Note : Assigning the evet to the delegate
public static event EventHandler _show;
static void Main(string[] args)
{
_show += new EventHandler(Dog);
_show += new EventHandler(Cat);
_show.Invoke();
}
static void Dog() {
Console.WriteLine("Doggie");
}
static void Cat(){
Console.WriteLine("Pussy");
}
}
`
如您所见,有几个函数称为 Dog / Cat。返回类型为void,但当您执行时,它看起来像string 值返回给事件_show。有人可以解释这里发生了什么吗?
【问题讨论】:
-
There return types are void but when you execute it looks like a string value is returned to the event _show你能解释一下你的意思吗?_show.Invoke()方法不返回任何内容,_show()也不返回。它只是无效的。 -
你是说
+=让你认为它是一个字符串? -
忘记我说的了,你能解释一下这里发生了什么吗?
-
OT:公共成员不应以
_开头。将其命名为event Show。