A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1860 below:

CA1860: Avoid using 'Enumerable.Any()' extension method - .NET

Property Value Rule ID CA1860 Title Avoid using 'Enumerable.Any()' extension method Category Performance Fix is breaking or non-breaking Non-breaking Enabled by default in .NET 9 As suggestion Cause

Enumerable.Any is called on a type that has a Length, Count, or IsEmpty property.

Rule description

To determine whether a collection type has any elements, it's more efficient and clearer to use the Length, Count, or IsEmpty (if possible) properties than to call the Enumerable.Any method.

Any(), which is an extension method, uses language integrated query (LINQ). It's more efficient to rely on the collection's own properties, and it also clarifies intent.

How to fix violations

Replace a call to Any() with a call to the collection's Length, Count, or IsEmpty property.

Example

The following code snippet shows a violation of CA1860:

bool HasElements(string[] strings)
{
    return strings.Any();
}
Function HasElements(strings As String()) As Boolean
    Return strings.Any()
End Function

The following code snippet fixes the violation:

bool HasElements(string[] strings)
{
    return strings.Length > 0;
}
Function HasElements(strings As String()) As Boolean
    Return strings.Length > 0
End Function
When to suppress warnings

It's safe to suppress this warning if performance isn't a concern.

Suppress a warning

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 CA1860
// The code that's violating the rule is on this line.
#pragma warning restore CA1860

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1860.severity = none

For more information, see How to suppress code analysis warnings.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4