I have a complex object I want to check is valid. I have a method to check it, but C# nullability checks don't know about them:
#nullable enable
...
bool IsValid(Thing? thing)
{
return thing?.foo != null && thing?.bar != null;
}
...
if(IsValid(thing))
{
thing.foo.subProperty; // CS8602 warning: foo is possibly null here
Is there any way to apply something like TypeScript's type-gates here? If IsValid is true then we know that thing.foo is not null, but the C# analyser can't see inside the function. If I inline the check it can, but then I'm copy-pasting code and this is a simple example.
Is there any annotation or pattern where I can have the nullability analysis with the more complex checks?
MemberNotNullWhen, but that will only work ifIsValidis made a method ofThing. - Jeroen Mostertthingis not null andthing.propertyis not null when it passes the validity check, it looks like I can do one or the other but not both. - Keith