Can I tell C# nullable references that a method is effectively a null check on a field
I looked around at the different attributes from System.Diagnostics.CodeAnalysis
and I couldn't find anything applicable, which is very disappointing. The closest you can get to what you want appears to be:
public bool TryGetName([NotNullWhen(true)] out string? name)
{
name = Name;
return name != null;
}
public void NameToUpperCase()
{
if (TryGetName(out var name))
{
Name = name.ToUpper();
}
}
It looks pretty cumbersome, I know. You can look at the MSDN docs for nullable attributes, maybe you'll find something neater.