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 type, is:
public static V? TryGetValue<K, V>(
this Dictionary<K, V> dict, K key) where V : struct =>
dict.TryGetValue(key, out V v)) ? new V?(v) ...
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 items in a dictionary and find myself converting keys or values to lists etc to do this better.
Actually, it's pretty easy to iterate over a dictionary, since ...
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 should have been a class. Or the dictionary itself may be right as a core storage method but there should be a wrapper class around it to provide the desired ...
Some good answers here on the general principles of hashtables/dictionaries. But I thought I'd touch on your code example,
if (dict.TryGetValue("key", out x))
As of C# 7 (which I think is around two years old), that can be simplified to:
if (dict.TryGetValue("key", out var x))
And of course ...
IMHO form a purely theoretical point of view, TryGetValue and other ThisAndThat methods is a code smell by itself, it is a method/function that does more than one thing. If methods/functions were composable you would use one to get the value and one to decide what to replace the null with or to handle the exception. The API is flawed in 2019. But it is what ...
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 write code like the following in Scala:
The second is returning a user-specified default value if the key isn't found:
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 should you be sure you are using the correct one for your situation. I think I know where the rumor of there being a problem using an out parameter comes from, so I'...
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 HashMap.
The problem to consider is what happens when a key does not exist. Some languages behave by returning null or nil or some other equivalent value. ...
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 dirty" approach might be to use ContainsKey() as a check instead.