[nullable-analysis] Make a non-nullable parameter, field, or property do not need to be initialized. #68758
-
I have a property which is non-nullable and normally I have to initialize it on exit from the constructor and I want to remove this warning. Since this property is in the ViewModel, it's available through Messenger, so it can't actually be uninitialized when it's actually called. public class ViewModel : IRecipient<PropertyChangedMessage<Foo?>>
{
public Foo MyFoo { get; set; }
public void Receive(PropertyChangedMessage<Foo?> message)
{
if (message.NewValue is null)
{
MyFoo = new Foo();
}
else
{
MyFoo = message.NewValue;
}
}
} How can this warning be removed without introducing new attribute? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Probably the easiest approach is to assign a default value of public Foo MyFoo { get; set; } = null!; |
Beta Was this translation helpful? Give feedback.
-
You should also take a look here. Specifically [MemberNotNull]. I realize after typing this that this is not really applicable, but it's good to know regardless! |
Beta Was this translation helpful? Give feedback.
Probably the easiest approach is to assign a default value of
null!
to the property: