使用EventHandler和仅定义事件在功能上有区别吗?
These are two different things
event
declares an event :
https://msdn.microsoft.com/en-us/library/8627sbea.aspx
EventHandler
defines the method that an event will raise:
https://msdn.microsoft.com/en-us/library/system.eventhandler(v=vs.110).aspx
EventHandler<TEventArgs>
is a convenient wrapper for your custom EventArgs
:
https://msdn.microsoft.com/en-us/library/db0etb8x(v=vs.110).aspx
Different ways to subscribe to an event
using System;
namespace ConsoleApplication3
{
internal class Program
{
private static void Main(string[] args)
{
// using a method
MyEvent += Program_MyEvent;
// using EventHandler, possible but not mandatory
MyEvent += new EventHandler(Target);
// using lambda syntax
MyEvent += (sender, eventArgs) => { };
// using delegate
MyEvent += delegate (object sender, EventArgs eventArgs) { };
// using delegate, signature is optional actually
MyEvent += delegate { };
}
private static void Target(object sender, EventArgs eventArgs)
{
}
private static void Program_MyEvent(object sender, EventArgs e)
{
}
public static event EventHandler MyEvent;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.