1
阅读事件描述和msdn的示例我可以看到事件订阅方式的差异。有时事件处理程序是“按原样”传递的,而有时是通过使用处理程序方法实例化委托来传递的。将代表传递给事件c#
...
class Subscriber
{
private string id;
public Subscriber(string ID, Publisher pub)
{
id = ID;
// Subscribe to the event using C# 2.0 syntax
pub.RaiseCustomEvent += HandleCustomEvent;
}
// Define what actions to take when the event is raised.
void HandleCustomEvent(object sender, CustomEventArgs e)
{
Console.WriteLine(id + " received this message: {0}", e.Message);
}
}
VS
public delegate void EventHandler1(int i);
...
public class TestClass
{
public static void Delegate1Method(int i)
{
System.Console.WriteLine(i);
}
public static void Delegate2Method(string s)
{
System.Console.WriteLine(s);
}
static void Main()
{
PropertyEventsSample p = new PropertyEventsSample();
p.Event1 += new EventHandler1(TestClass.Delegate1Method);
p.RaiseEvent1(2);
...
}
}
是否有人可以提供这方面的透明度?
谢谢。
我不知道“语法糖”是什么,但它听起来像是我将来使用更多的词组:) :) – Nate
@Nate一勺代码/医药无论做什么都做同样的工作。但是添加一点糖使其变得容易很多。 –
3.5用lamda表达式增加了一些“语法糖”:) – Wanabrutbeer