C
C#2y ago
Jwan

❔ 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
Thinker
Thinker2y ago
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.
Jwan
JwanOP2y ago
Setting them as Required doesn't seem to have helped
Thinker
Thinker2y ago
Not the attribute That doesn't do anything public required string ImageUrl { get; set; }
Jwan
JwanOP2y ago
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?
Thinker
Thinker2y ago
New in C# 11
Jwan
JwanOP2y ago
Awesome, good to know. Thanks again 🙏
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server