Suppose we have this MCVE
class Person
{
public int? Age;
public string? Name;
public bool IsInvalid => Name == null || Age == null;
// ...
public void Something(Person other)
{
if (other.IsInvalid)
return;
Console.WriteLine(other.Name);
Console.WriteLine(other.Age);
}
}
This would complain to me because the compiler would rather have the expression for the property inside the if
statement.
In my actual code, I have conditions like this but the property makes it more readable than checking for a series of nulls. Is there some attribute or something I can do to make the compiler realize that the property gives me non-null values?
I also fully understand that this might be a non-trivial task for a compiler to implement and as such it may not be possible right now.
This is also being done via .NET Core 3.0 Preview 9, so at the time of writing I have access to all the attributes that may exist.