Reputation: 149
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;
}
}
Upvotes: 13
Views: 8652
Reputation: 63
Initialize the property to the default.
public string name { get; set; } = default!;
Upvotes: -1
Reputation: 456
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;
Upvotes: 17