C#


ROSLYN.PERFORMANCE.CA1854 : Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method (C#)

See Roslyn-Detected C# Warning Classes for more information.

Summary

Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check. 'ContainsKey' and the indexer both would lookup the key under the hood, so using 'TryGetValue' removes the extra lookup.

This check corresponds to Roslyn rule CA1854. For full rule details, see the Microsoft website: CA1854.

Properties

Class Name Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method (C#)
Significance reliability
Mnemonic ROSLYN.PERFORMANCE.CA1854
Categories None
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method (C#)"

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.