DenisSarrazin
DenisSarrazin

Reputation: 63

Difference between (args is { Length: > 0}) and args.Length?

I was going through some of the code in the dotnet runtime, and I've noticed that instead of writing something like this:

if (args.Length > 0)

they use this:

if (args is { Length: > 0})

Do you know if there are any advantages of using the second method instead of the first? Seems longer and less easy to read but for some reason, the second method is used?

Upvotes: 6

Views: 829

Answers (1)

Orion
Orion

Reputation: 822

If args is null, then args.Length > 0 throws a NullReferenceException.

In the same situation, args is { Length: > 0} simply evaluates to false.

Upvotes: 7

Related Questions