using System;
public delegate void LogEventHandler(object source, LogEventArgs args);
public class EventDemo
{
public event LogEventHandler logEvent;
public void Do()
{
logEvent(this,new LogEventArgs());
Console.WriteLine("Ok.");
}
}
public class LogEventArgs : EventArgs
{
public LogEventArgs()
{}
}
public class App
{
public static void Main()
{
EventDemo eventDemo =new EventDemo();
eventDemo.logEvent += new LogEventHandler(WriteLog);
eventDemo.Do();
}
public static void WriteLog(object sender,LogEventArgs le)
{
Console.WriteLine("In Event :Write Done. ");
}
}
本文介绍了一个简单的C#事件和委托的使用案例。通过定义委托类型和事件,演示了如何触发事件及订阅事件处理程序。当事件被触发时,控制台会输出特定的消息。
643

被折叠的 条评论
为什么被折叠?



