Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sender of static EventHandler event

Tags:

c#

.net

events

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?

like image 625
Alex F Avatar asked Jul 25 '13 11:07

Alex F


People also ask

Is an EventHandler a delegate?

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.

What does static event mean?

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.

Which is the best place to subscribe event handler to an event?

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.

What is C# EventHandler?

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.


1 Answers

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)

like image 125
oakio Avatar answered Oct 19 '22 08:10

oakio