I have class with static EventHandler
event:
public static event EventHandler MyEvent;
static void RaiseEvent()
{
EventHandler p = MyEvent;
if (p != null)
{
p(null, EventArgs.Empty);
}
}
Since I don't have any this
object which can be used as event sender, I raise this event with sender = null
. Is it OK to have this parameter set to null, according to .NET programming guidelines? If not, what object can I use as a sender?
The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.
With a static event, there is no sending instance (just a type, which may or may not be a class). There still can be a recipient instance encoded as the target of the delegate.
To subscribe to events by using the Visual Studio IDEOn top of the Properties window, click the Events icon. Double-click the event that you want to create, for example the Load event. Visual C# creates an empty event handler method and adds it to your code. Alternatively you can add the code manually in Code view.
An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.
Event Design
On static events, the sender parameter should be null
Source: https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms229011(v=vs.100)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With