Skip to content

Prefer "Any" to "Count() > 0" #490

@giggio

Description

@giggio

If you find a call to Count() > 0 and the object supports Any(), change it Any().
If you find a call to Count(predicate) > 0 and the object supports Any(<predicate type>), change it Any(predicate).

So this:

var any = myEnumerable.Count() > 0;

Becomes:

var any = myEnumerable.Any();

And:

var any = myEnumerable.Count(i => i > 3) > 0;

Becomes:

var any = myEnumerable.Any(i => i > 3);

Diagnostic id: CC0099
Severity: Information
Category: Style

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions