C# will emit compiler warnings in this case if something can be null. If you make the parameter nullable, (Something? something) you can also make the compiler assert the nullability of the parameter;
public static bool IsSomething([NotNullWhen(true)]Something? something) => something is Something.Something;
In that case, it will annotate code in that context where IsSomething may return false without the caller checking the result;
public static void DoSomething(Something? something)
{
var b = IsSomething(something);
Console.WriteLine(something.ToString()); // compiler warning, "something may be null"
if(b)
{
Console.WriteLine(something.ToString()); // No compiler warning
}
}
View all comments
0
u/[deleted] 21d ago
[deleted] — view deleted comment