How is args is { Length: > 0 }
better than args.Length > 0
#65385
Answered
by
stephentoub
maxkoshevoi
asked this question in
Q&A
-
I came across this code, and am a bit confused about why pattern matching is used here. |
Beta Was this translation helpful? Give feedback.
Answered by
stephentoub
Feb 15, 2022
Replies: 1 comment 6 replies
-
There's a functional difference. if (args is { Length: > 0 }) is the same as: if (args is not null && args.Length > 0) whereas if (args.Length > 0) will null ref if (But with the lines you cited, there's no way that null check will have a measurable impact on performance.) |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
maxkoshevoi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a functional difference.
is the same as:
whereas
will null ref if
args
is null. Ifargs
can't be null,if (args.Length > 0)
is a tad better. If it can be null,if (args is { Length: > 0 })
is clearly better.(But with the lines you cited, there's no way that null check will have a measurable impact on performance.)