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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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> ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
out-parameters × 6java × 2
coding-standards × 2
mutable × 2
c# × 1
c++ × 1
javascript × 1
coding-style × 1
documentation × 1
functions × 1
anti-patterns × 1
readability × 1
code-smell × 1
parameters × 1
dictionary × 1