Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Non-nullable event must contain a non-null value when exiting constructor"

Im getting the warning "Non-nullable event 'SomeEvent' must contain a non-null value when exiting constructor. Consider declaring the event as nullable."

Here's a very simplified version of my code which replicates the exact same problem. What am I missing here? Does this have anything to do with .Net 6?

namespace ConsoleApp3
{
    public delegate void SomeDelegate(object sender, EventArgs args);

    public class NewClass
    {
        public NewClass(string name)
        {
            this.name = name;

        }

        public string name { get; set; }

        public event SomeDelegate SomeEvent;
    }
}
like image 862
ruwenzori Avatar asked Feb 23 '26 15:02

ruwenzori


1 Answers

I know I'm late to the party, but Google sent me here, and the only responses were unsatisfactory. I came upon another answer on StackOverflow that felt a lot better, and you can get a good explanation there.

tl;dr just make the event nullable, because that's what it actually is:

public event SomeDelegate? SomeEvent;  
like image 95
zfrank Avatar answered Feb 26 '26 05:02

zfrank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!