❔ Non-nullable property 'Name' must contain a non-null value when exiting constructor
In the following class, why do I get this warning and how do I make it happy? I supposed I could make it like
string? Name
but that isn't really accurate because I can never have a PlayerModel where the Name is null
. Same for ImageUrl
7 Replies
You could make them
required
You're getting the error because the types of the properties can't be null but they may be when exiting the constructor.Setting them as
Required
doesn't seem to have helpedNot the attribute
That doesn't do anything
public required string ImageUrl { get; set; }
Thanks @thinker227 That fixed it. I don't think I've seen much of
public required string
, is that new? Or has it always been a feature of the language?New in C# 11
Awesome, good to know. Thanks again 🙏
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.