ARTICLE AD BOX
I'm working with a library that has some "Try" methods that are not properly annotated to pass nullability information. This becomes a hassle when the compiler is not able to determine if the variable is or isn't null.
// signature: bool TryGetProperty(string propName, out string? value); // (no NotNullWhen(true) attribute) if (!obj.TryGetProperty(propName, out var value)) { // fail case throw ...; } // compiler doesn't know that `value` cannot be null at this point UseTheValue(obj, value!); // have to add '!' to dismiss the warnings AnotherUseOfValue(obj, value!); // other uses doesn't affect the nullabilityIt seems the only ways to make the compiler know it's not null is if I were to add an extra explicit null check elsewhere or access a property off the object in the scope. Problem is, I won't always operate on the object directly in the surrounding code.
if (value is not null) // redundant AnotherUseOfValue(obj, value); // value is not null now //or _ = value.someProperty; // value cannot be null if this succeedsAre there ways I can restore this null check without having to add it explicitly, without updating the library to add the proper annotation? Turning off the warnings is not an option.
This looks less problematic, but I don't like it:
if (!obj.TryGetProperty(propName, out var value) || value is null) // add extraneous check here { // fail case throw ...; }