Is there a way to tell a compiler that argument's property is not null if method returns #6064
Answered
by
Joe4evr
maxkoshevoi
asked this question in
Q&A
-
I have code like this: bool CheckProperty(MyType instance) => instance.A?.B?.C != null;
void Main()
{
if (CheckProperty(instance))
{
Console.WriteLine(instance.A.B.C); // <- Warning here
}
} Is there a way to annotate |
Beta Was this translation helpful? Give feedback.
Answered by
Joe4evr
Apr 26, 2022
Replies: 2 comments
-
No, not currently. The list of possible annotations is at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis. |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can't use the attributes to inform null analysis beyond the scope of the current type. if (instance.A?.B?.C is {} c)
{
Console.WriteLine(c);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
maxkoshevoi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can't use the attributes to inform null analysis beyond the scope of the current type.
The best you'll get here today is