Rule Definition
The Count and LongCount LINQ method calls require enumerating the entire collection to compute the count. The same check is faster with the Any method as it avoids enumerating the collection.
The Any method make the code more efficient and more readable.
Remediation
Use Any method instead of Count and LongCount if possible.
Violation Code Sample
private bool isNotEmpty(IEnumerable<string> strings)
{
return strings.Count() > 0; // >= 1, == 0, <= 1, ...
}
private bool isNotEmpty(IEnumerable<string> strings)
{
return strings.LongCount() > 0; // >= 1, == 0, <= 1, ...
}
Fixed Code Sample
private bool isNotEmpty(IEnumerable<string> strings)
{
return strings.Any();
}
Reference
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1827?view=vs-2019
Related Technologies
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.