I have a class with an event like this
public class A
{
public event Func<string, string> Message;
public void Calling()
{
Message("Hello world!");
}
}
If I call the Calling()
method and no one has subscribed to the Message
event yet, it is null and throws an exception.
How can I initialize my event?
You don't init your event rather you need to check for null in your Calling method:
public void Calling()
{
if (Message != null)
Message("Hello World!");
}
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