Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåDenne nettleseren støttes ikke lenger.
Oppgrader til Microsoft Edge for å dra nytte av de nyeste funksjonene, sikkerhetsoppdateringene og den nyeste tekniske støtten.
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 |
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.
Calling Contains
on the Keys
or Values
collection can often be more expensive than calling ContainsKey
or ContainsValue
on the dictionary itself:
Keys
or Values
collection may result in extra allocations.Contains
extension method on IEnumerable<T> usually does a slow O(n) containment check.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
It is safe to suppress warnings from this rule if the code in question is not performance-critical.
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.
.NET-tilbakemelding
.NET er et åpen kilde-prosjekt. Velg en kobling for å gi tilbakemelding:
Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nå