Evenimente
17 mar., 21 - 21 mar., 10
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acumAcest browser nu mai este acceptat.
Faceți upgrade la Microsoft Edge pentru a profita de cele mai noi funcții, actualizări de securitate și asistență tehnică.
Property | Value |
---|---|
Rule ID | CA1827 |
Title | Do not use Count()/LongCount() when Any() can be used |
Category | Performance |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | As suggestion |
The Count() or LongCount() method was used where the Any() method would be more efficient.
This rule flags Count() and LongCount() LINQ method calls that are used to check if the collection has at least one element. These methods enumerate the entire collection to compute the count. The same check is faster with the Any() method as it avoids enumerating the collection.
Notă
This rule is similar to CA1860: Avoid using 'Enumerable.Any()' extension method. However that rule suggests using the Count
property, while this rule applies to the Linq Count()
extension method.
To fix a violation, replace the Count or LongCount method call with the Any method. For example, the following two code snippets show a violation of the rule and how to fix it:
using System.Collections.Generic;
using System.Linq;
class C
{
public string M1(IEnumerable<string> list)
=> list.Count() != 0 ? "Not empty" : "Empty";
public string M2(IEnumerable<string> list)
=> list.LongCount() > 0 ? "Not empty" : "Empty";
}
using System.Collections.Generic;
using System.Linq;
class C
{
public string M1(IEnumerable<string> list)
=> list.Any() ? "Not empty" : "Empty";
public string M2(IEnumerable<string> list)
=> list.Any() ? "Not empty" : "Empty";
}
Sfat
A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press Ctrl+. (period). Choose Do not use Count() or LongCount() when Any() can be used from the list of options that's presented.
It's safe to suppress a violation of this rule if you're not concerned about the performance impact from unnecessary collection enumeration to compute the count.
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1827
// The code that's violating the rule is on this line.
#pragma warning restore CA1827
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1827.severity = none
For more information, see How to suppress code analysis warnings.
Feedback pentru .NET
.NET este un proiect open source. Selectați un link pentru a oferi feedback:
Evenimente
17 mar., 21 - 21 mar., 10
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acum