The null check that didn't check for nulls
I wrote the following code:
if (_items is [var single])
{
// no point invoking thread pool
single.Run();
}
And I was very proud of myself for writing such pretty and succinct C# code.
Then I got a runtime error:
I asked Grok about this because I did not expect this, and got the following reply:
No,
if (_items is [var single])
in C# does not match a null value. This pattern checks if_items
is a single-element array and binds the element to single. If_items
isnull
, the pattern match fails, and the condition evaluates tofalse
.
However, the output clearly disagreed with both Grok’s and my expectations. I decided to put that into SharpLab, which can quickly help identify what is going on behind the scenes for such syntax.
You can see three versions of this check in the associated link.
if(strs is [var s]) // no null check
if(strs is [string s]) // if (s != null)
if(strs is [{} s]) // if (s != null)
Turns out that there is a distinction between a var pattern (allows null) and a non-var pattern. The third option is the non-null pattern, which does the same thing (but doesn’t require redundant type specification). Usually var
vs. type is a readability distinction, but here we have a real difference in behavior.
Note that when I asked the LLM about it, I got the wrong answer. Luckily, I could get a verified answer by just checking the compiler output, and only then head out to the C# spec to see if this is a compiler bug or just a misunderstanding.
Comments
That is surprising - I think of var as a shorthand that does not affect the final result of the compilation. I wouldn't expect a difference between these two statements:
but popping this into SharpLab.IO shows otherwise.
Comment preview
Join the conversation...