❔ Non-nullable field 'rating' must contain a non-null value when exiting constructor
Why am i getting this warning:
Non-nullable field 'rating' must contain a non-null value when exiting constructor
?
Before exiting the constructor Rating = aRating
is executed and thus rating should be set rating
to "G"
or "PG"
right?
4 Replies
It can't see that setting
Rating
always sets rating
under the hood
You can add [MemberNotNull(nameof(rating))]
to the setter to make the compiler happy
(I also wrote a switch with pattern matching instead of the if statement, you can use your original setter body if you don't understand or don't like how that looks)Thank you!
I haven't seen this way of writing a switch statement but it looks very concise. TY! 🙂
its a "switch expression". They are very very nice.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.