Analyze nullable return value statically? #46496
-
Beta Was this translation helpful? Give feedback.
Answered by
PathogenDavid
Jan 1, 2021
Replies: 1 comment 2 replies
-
I believe what you're looking for is the nullable analysis attributes. Specifically, [return: NotNullIfNotNull("obj")]
public static object? ToObject<T>(this T obj) => obj; As an aside: var obj1Result = obj1.ToObject(); // should be object With |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Cyl18
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe what you're looking for is the nullable analysis attributes.
Specifically,
NotNullIfNotNull
: (Full example on SharpLab here)As an aside:
With
var
this will always beobject?
, but the compiler will know it is not null. (Whether a particular variable is known to be null or able to be null are separate concepts.var
is always nullable, but may or may not be known to be null.)