Skip to content
Discussion options

You must be logged in to vote

It's intentional. v is declared as capable of holding string?, but the fact that the value it holds is not null is still tracked just the same up to the point where you assign something nullable to it. It's the same if you do this:

var v = foo.Value;

Console.WriteLine(v.Length); // No warning even though v is `string?` because it's tracked as not null

v = fooThatMightBeNull; // No warning here because v is `string?` not `string`

Console.WriteLine(v.Length); // Warning because it's tracked as maybe-null

Initially var did not work this way, but it became clear that this would be too painful for such a common scenario in the wild. If it wasn't for this design, then this (and non-loop vers…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mburbea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants