Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 92719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:13:51+00:00 2026-05-10T23:13:51+00:00

I just recently noticed Dictionary.TryGetValue(TKey key, out TValue value) and was curious as to

  • 0

I just recently noticed Dictionary.TryGetValue(TKey key, out TValue value) and was curious as to which is the better approach to retrieving a value from the Dictionary.

I’ve traditionally done:

if (myDict.Contains(someKey))      someVal = myDict[someKey];      ... 

unless I know it has to be in there.

Is it better to just do:

if (myDict.TryGetValue(somekey, out someVal)     ... 

Which is the better practice? Is one faster than the other? I would imagine that the Try version would be slower as its ‘swallowing’ a try/catch inside itself and using that as logic, no?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:13:52+00:00Added an answer on May 10, 2026 at 11:13 pm

    TryGetValue is slightly faster, because FindEntry will only be called once.

    How much faster? It depends on the dataset at hand. When you call the Contains method, Dictionary does an internal search to find its index. If it returns true, you need another index search to get the actual value. When you use TryGetValue, it searches only once for the index and if found, it assigns the value to your variable.

    FYI: It’s not actually catching an error.

    It’s calling:

    public bool TryGetValue(TKey key, out TValue value) {     int index = this.FindEntry(key);     if (index >= 0)     {         value = this.entries[index].value;         return true;     }     value = default(TValue);     return false; } 

    ContainsKey is this:

    public bool ContainsKey(TKey key) {     return (this.FindEntry(key) >= 0); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just recently noticed this behaviour. To put it shortly, it displays the commits
I've recently began using dTrace and have noticed just how awesome it is. Its
I just recently changed out all my hard drives and in the process of
I've just recently begun to use markdown and have noticed that any markdown within
I just noticed something particular. I have an internal stock service which is published
I've recently noticed a problem with styled <a> and <button> tags which occurs with
I am reading windows eventlog using WMI and just noticed recently that time is
We just recently upgraded from VS2005 to VS2010. I've noticed that unlike VS2005, when
We just recently upgraded our application and noticed that we're getting runtime errors on
I have just noticed recently that my page title will reset to the standard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.