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/ca1841 below:

CA1841: Prefer Dictionary Contains methods (code analysis) - .NET

Property Value Rule ID CA1841 Title Prefer Dictionary Contains methods Category Performance Fix is breaking or non-breaking Non-breaking Enabled by default in .NET 9 As suggestion Cause

This rule locates calls to a Contains method on the Keys or Values collection of an IDictionary<TKey,TValue> that could be replaced with a call to a ContainsKey or ContainsValue method on the dictionary itself.

Rule description

Calling Contains on the Keys or Values collection can often be more expensive than calling ContainsKey or ContainsValue on the dictionary itself:

How to fix violations

To fix violations, replace calls to dictionary.Keys.Contains or dictionary.Values.Contains with calls to dictionary.ContainsKey or dictionary.ContainsValue, respectively.

The following code snippet shows examples of violations, and how to fix them.

using System.Collections.Generic;
// Importing this namespace brings extension methods for IEnumerable<T> into scope.
using System.Linq;

class Example
{
    void Method()
    {
        var dictionary = new Dictionary<string, int>();

        //  Violation
        dictionary.Keys.Contains("hello world");

        //  Fixed
        dictionary.ContainsKey("hello world");

        //  Violation
        dictionary.Values.Contains(17);

        //  Fixed
        dictionary.ContainsValue(17);
    }
}
Imports System.Collection.Generic
' Importing this namespace brings extension methods for IEnumerable(Of T) into scope.
' Note that in Visual Basic, this namespace is often imported automatically throughout the project.
Imports System.Linq

Class Example
    Private Sub Method()
        Dim dictionary = New Dictionary(Of String, Of Integer)

        ' Violation
        dictionary.Keys.Contains("hello world")

        ' Fixed
        dictionary.ContainsKey("hello world")

        ' Violation
        dictionary.Values.Contains(17)

        ' Fixed
        dictionary.ContainsValue(17)
    End Sub
End Class
When to suppress warnings

It is safe to suppress warnings from this rule if the code in question is not performance-critical.

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

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

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

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

See also

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