Skip to content

Prefer Dictionary<K, V>.TryGetValue() over guarded indexer access #33798

Closed
dotnet/roslyn-analyzers
#4851

Description

@terrajobst

Dictionary<K, V>.ContainsKey(key) followed by Dictionary<K, V>.this[key] can be combined into a single TryGetValue call.

Category: Performance

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. added this to the Future milestone on Mar 20, 2020
  4. added
    code-fixer
    on Mar 21, 2020
  5. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Medium
    • Fixer: Medium
  6. removed
    untriaged
    on Mar 21, 2020
  7. carlossanlop commented on Jan 8, 2021

    @carlossanlop
    Contributor

    Suggested severity: info

    // Before
            public int TryGet(Dictionary<string, int> data, string key)
            {​​
                if (data.ContainsKey(key))
                {
                    return data[key];
                }
                return 0;
            }​​
    // After
            public int TryGet(Dictionary<string, int> data, string key)
            {​​
                if (data.TryGetValue(key, out int value))
                {
                    return value;
                }
                return 0;
            }​​

    It may be possible to preserve unrelated code inside the if statement as long as key is used without modifying the dictionary:

    // Before
            public int TryGet(Dictionary<string, int> data, string key)
            {​​
                if (data.ContainsKey(key))
                {
                    Foo(data[key]); // Unrelated code
                    return data[key];
                }
                return 0;
            }​​
    // After
            public int TryGet(Dictionary<string, int> data, string key)
            {​​
                if (data.TryGetValue(key, out int value))
                {
                    Foo(value); // Unrelated code
                    return value;
                }
                return 0;
            }​​

    Avoid triggering if the data is modified before returning:

            public int DontTrigger(Dictionary<string, int> data, string key)
            {​​
                if (data.ContainsKey(key))
                {
                    data[key] = DoSoemthing(data[key]); // Unrelated code
                    return data[key];
                }
                return 0;
            }​​
  8. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jan 15, 2021
  9. added
    api-approved
    and removed
    api-ready-for-review
    on Feb 4, 2021
  10. terrajobst commented on Feb 4, 2021

    @terrajobst
    ContributorAuthor

    Video

  11. added
    help wanted
    on Feb 4, 2021
  12. CollinAlpert commented on Feb 7, 2021

    @CollinAlpert
    Contributor

    I'd like to do this one. @carlossanlop

  13. removed
    help wanted
    on Feb 8, 2021
  14. carlossanlop commented on Feb 8, 2021

    @carlossanlop
    Contributor

    It's yours, @CollinAlpert . Thanks for offering your help!

  15. modified the milestones: Future, 7.0.0 on Aug 20, 2021
  16. ghost locked as resolved and limited conversation to collaborators on Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Collectionscode-analyzercode-fixer

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions