I have some shorthand for setting property values in a Xamarin app:
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
This is used in my ViewModels, for example:
class PersonViewModel : ViewModel
{
private string name;
public string Name
{
get => name;
set => SetProperty(ref name, value);
}
public PersonViewModel(string theName) {
Name = theName;
}
}
When I have #nullable enable, how can I (in the general case, whenever SetProperty is used) help the compiler infer that the name member is not null after construction?
Currently, I get a warning on the constructor which says: "CS8618: Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.".
I'm using <LangVersion>latest</LangVersion> in my .csproj.