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

  • Home
  • SEARCH
  • 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 8481707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:37:52+00:00 2026-06-10T19:37:52+00:00

If the Dictionary<TKey, TValue>.TryGetValue method returns true , the parameter value contains the value

  • 0

If the Dictionary<TKey, TValue>.TryGetValue method returns true, the parameter value contains the value associated with the specified key.

I have read some examples of changing the value associated with the specified key: after invoking the TryGetValue method, the updating of the value associated with the key is performed by the indexer, resulting in a further access to the Dictionary (see the following code).

var d = new Dictionary<string, MyClass>();
...
MyClass obj;
if (d.TryGetValue(key, out obj))
{
    d[key].Update(...); // update the value
}

The obj object is a reference to the value associated with the key. Why not directly use the object to update the value, as in the code below?

MyClass obj;
if (d.TryGetValue(key, out obj))
{
    obj.Update(...); // update the value
}
  • 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. Editorial Team
    Editorial Team
    2026-06-10T19:37:54+00:00Added an answer on June 10, 2026 at 7:37 pm

    Example code is not the same as real code. It demonstrates a given point, and nothing else. This can mean lots of examples are sub-optimal (can we all promise never to use ToList() in linq example code that doesn’t actually need it? I’m convinced that’s why it’s so often used when the sole effect is to make the code slower) and very many that are pointless.

    This isn’t even a bad thing. Commenting in example code will describe the things that are being explained, which in real code we should expect people to understand. Meanwhile it won’t document why you’re doing some weird-looking thing because that’ll either be in the surrounding text or the answer is simply “because it demonstrates this feature, nothing more”. In real code, anything that is surprising or weird-looking should always be explained in comments (as well as the obvious reason, if you can’t write a good explanation of the weirdness, you aren’t as justified in that weirdness as you thought you were). While some junior developers comments may suggest they’re doing the former in real code, the fact is that we want comments in examples to explain what would be obvious to someone who knows the features in question – because we’re not faimilar with them, and that’s what we’re reading the example for,

    With a mutable reference type, the second version is indeed more sensible, and frankly a better example, but the author was presumably just trying to think about yet another example of yet another feature which is a different mental pressure to be under than that we are under when writing real code. The opposite in some ways (we want to favour the well-known over the exotic, while a tutorial writer has to use every single feature in order to explain them).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just recently noticed Dictionary.TryGetValue(TKey key, out TValue value) and was curious as to
For a small set of key/value pairs (default 2, max 5), a Dictionary<TKey, TValue>
Dictionary<TKey, TValue>.KeyCollection Class implements the contains through extension method from IEnumerable , which is
I have used Dictionary(TKey, TValue) for many purposes. But I haven't encountered any scenario
In .NET, there is a constructor for Dictionary<TKey, TValue> that takes one parameter, int
SortedList<TKey, TValue>.IndexOfKey(key) returns -1 if key is not in the list. Does this mean
If I have access only keys from a Dictionary<TKey, TValue> what is better to
I have a Enumerable<KeyValuePair<TKey, TValue>> . I want to create a bool TryGetValue(TKey, out
I've created this extension method public static void AddIfNullCreate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey
I have a Dictionary(TKey, TValue) like Dictionary<int, ArrayList> Deduction_Employees = new Dictionary<int, ArrayList>(); and

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.