using System;
namespace ConsoleApp_CS
{
class 小弟
{
//1.聲明事件實例
public event EventHandler 挨扁;
//2.包含事件
private void 挨扁了(EventArgs e)
{
if(this.挨扁 != null)
this.挨扁(this, e);
}
//3.觸發事件
public void 被扁()
{
Console.WriteLine("被扁ing...");
this.挨扁了(EventArgs.Empty);
}
}
class 我
{
private 小弟 我小弟 = null;
public 我(小弟 小小弟)
{
this.我小弟 = 小小弟;
//註冊事件
this.我小弟.挨扁 += new EventHandler(this.担心);
}
public void 担心(object sender, EventArgs e)
{
Console.WriteLine("我担心");
}
}
class InstanceDelegate
{
[STAThread]
public static void Main ()
{
小弟 小弟弟 = new 小弟();
我 大哥 = new 我(小弟弟);
小弟弟.被扁();
Console.ReadLine();
} //end of main
}
}
此博客展示了一段 C# 代码,定义了一个名为“小弟”的类,声明了“挨扁”事件,包含触发事件的方法。还定义了“我”类,在构造函数中注册“挨扁”事件。最后在 Main 方法中创建对象并触发事件,体现了 C# 事件机制。
1907

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



