In C# I can check if an event has any listeners:
C# Example:
public static event EventHandler OnClick;
if (OnClick != null)
OnClick(null, new EventArgs() );
In C++/CLI checking if the event is null is not necessary.
C++/CLI Example:
delegate void ClickDelegate( Object^ sender, MyEventArgs^ e );
event ClickDelegate^ OnClick;
OnClick (sender, args);
BUT, in the project I am working on, I don’t want to construct the MyEventArgs object if there are no listeners.
How do I find out if OnClick has any listeners in C++?
It seems you can’t check that with “trivial events”, like you used, because you don’t have direct access to the underlying field (as with auto-implemented properties in C#).
If you want to do this, you can specify the event’s accessor methods and the backing field explicitly. See How to: Define Event Accessor Methods on how exactly to do that.