Skip to content

How is args is { Length: > 0 } better than args.Length > 0 #65385

Answered by stephentoub
maxkoshevoi asked this question in Q&A
Discussion options

You must be logged in to vote

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 args is null. If args 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.)

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@stephentoub
Comment options

@maxkoshevoi
Comment options

@stephentoub
Comment options

@maxkoshevoi
Comment options

@stephentoub
Comment options

Answer selected by maxkoshevoi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants