Skip to main content
35 votes

Is there a better way to use C# dictionaries than TryGetValue?

Some good answers here on the general principles of hashtables/dictionaries. But I thought I'd touch on your code example, int x; if (dict.TryGetValue("key", out x)) { DoSomethingWith(x); } As ...
David Arno's user avatar
  • 39.6k
30 votes
Accepted

Is there a better way to use C# dictionaries than TryGetValue?

Dictionaries (C# or otherwise) are simply a container where you look up a value based on a key. In many languages it's more correctly identified as a Map with the most common implementation being a ...
Berin Loritsch's user avatar
18 votes

Is there a better way to use C# dictionaries than TryGetValue?

This is neither a code smell nor an anti-pattern, as using TryGet-style functions with an out parameter is idiomatic C#. However, there are 3 options provided in C# to work with a Dictionary, so ...
BrianH's user avatar
  • 6,140
13 votes

Is there a better way to use C# dictionaries than TryGetValue?

There are at least two methods missing from C# dictionaries that in my opinion clean up code considerably in a lot of situations in other languages. The first is returning an Option, which lets you ...
Karl Bielefeldt's user avatar
8 votes

Is there a better way to use C# dictionaries than TryGetValue?

That's 4 lines of code to essentially do the following: DoSomethingWith(dict["key"]) I agree that this is inelegant. A mechanism that I like to use in this case, where the value is a struct ...
Eric Lippert's user avatar
  • 46.6k
5 votes
Accepted

Should a method modifying object passed as a parameter return the modified object?

I think you have this backwards. The only thing that private void normalizeDimensions(Request request); can do is to modify the object (or have other side effects), so it's clear that's what this ...
Philip Kendall's user avatar
4 votes

Is there a better way to use C# dictionaries than TryGetValue?

Other answers contain great points, so I won't restate them here, but instead I'll focus on this part, which seems to be largely ignored so far: Similarly, I often would like to iterate through the ...
Vilx-'s user avatar
  • 5,440
3 votes

Is there a better way to use C# dictionaries than TryGetValue?

The TryGetValue() construct is only necessary if you don't know whether "key" is present as a key within the dictionary or not, otherwise DoSomethingWith(dict["key"]) is perfectly valid. A "less ...
Peregrine's user avatar
  • 1,246
2 votes

Should a method modifying object passed as a parameter return the modified object?

In the functional world Philip Kendall is entirely correct. And in the OOP world we occasionally write functional code as well. So if you're going to make Request immutable then: private Request ...
candied_orange's user avatar
2 votes

Is there a better way to use C# dictionaries than TryGetValue?

If you feel using a dictionary is awkward it may not be the right choice for your problem. Dictionaries are great but like one commenter noticed, often they are used as a shortcut for something that ...
Martin Maat's user avatar
  • 18.6k
1 vote

Is there a better way to use C# dictionaries than TryGetValue?

This was bugging me too, so I came up with this extension: public static class DictionaryExtension { public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> ...
Philip Claren's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible