Nat*_*ate 1 c# events delegates
阅读事件描述和msdn示例我可以看到事件订阅方式的差异.有时事件处理程序"按原样"传递,有时它们通过使用处理程序方法实例化委托来传递它们,例如
...
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
...
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以提供清楚吗?
谢谢.
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |