24

What does the [string] indexer of Dictionary return when the key doesn't exist in the Dictionary? I am new to C# and I can't seem to find a reference as good as the Javadocs.

Do I get null, or do I get an exception?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
jjnguy
  • 136,852
  • 53
  • 295
  • 323

4 Answers4

25

If you mean the indexer of a Dictionary<string,SomeType>, then you should see an exception (KeyNotFoundException). If you don't want it to error:

SomeType value;
if(dict.TryGetValue(key, out value)) {
   // key existed; value is set
} else {
   // key not found; value is default(SomeType)
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
14

As ever, the documentation is the way to find out.

Under Exceptions:

KeyNotFoundException
The property is retrieved and key does not exist in the collection

(I'm assuming you mean Dictionary<TKey,TValue>, by the way.)

Note that this is different from the non-generic Hashtable behaviour.

To try to get a key's value when you don't know whether or not it exists, use TryGetValue.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks, The .net documentation is still kinda hard for me to wade through and find the info I need. – jjnguy Dec 29 '08 at 14:10
  • 1
    It's mostly pretty good (particularly the offline version, IMO - use the Index) but you need to know that to find the indexer documentation you usually look for the Item property. – Jon Skeet Dec 29 '08 at 14:12
  • Index...ok, I will remember this. I prefer dict.get(key) from Java. Oh well...I'm kind of a fan-boy. – jjnguy Dec 29 '08 at 14:23
  • Item, not Index. As for preferring the Java way - give it time. C# is a *much* nicer language than Java IMO. It takes a while to adjust, especially with the various new features of C# 3.0, but it's lovely :) – Jon Skeet Dec 29 '08 at 15:19
  • You can use either Google (http://www.google.com/search?hl=en&q=system.collections.generic.dictionary+site%3Amsdn.microsoft.com) or MSDN (http://social.msdn.microsoft.com/Search/en-US/?query=system.collections.generic.dictionary&ac=8) to search the MSDN documentation. – jason Dec 29 '08 at 15:24
  • I've been working in C# for 7 years and didn't find it either. Didn't know it was referred to as Item. I seem to remember indexers used to just be explicitly listed on the class or members page. Thanks for pointing that out in your follow up @JonSkeet – xr280xr Jan 16 '13 at 20:00
  • The question specifically mentions Dictionary, but it might be worth noting that the IDictionary documentation for the item property does not mention the KeyNotFoundException exception, and only specifies NotSupportedException when setting the value corresponding to a key which does not exist (http://msdn.microsoft.com/en-us/library/system.collections.idictionary.item(v=vs.100).aspx). The Dictionary<> generic class implements the IDictionary<> interface, but luckily LINQ returns an instance of the class from ToDictionary(). – Greg Jun 11 '14 at 18:18
  • @Greg: You've linked to the documentation for the non-generic interface. The [generic `IDictionary<,>` indexer documentation](http://msdn.microsoft.com/en-us/library/zyxt2e2h(v=vs.110).aspx) *does* talk about `KeyNotFoundException`. – Jon Skeet Jun 11 '14 at 18:19
5

I think you can try a

dict.ContainsKey(someKey)

to check if the Dictionary contains the key or not.

Thanks

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Viking22
  • 545
  • 1
  • 7
  • 19
4

Alternatively to using TryGetValue, you can first check if the key exists using dict.ContainsKey(key) thus eliminating the need to declare a value prior to finding out if you'll actually need it.

Kon
  • 27,113
  • 11
  • 60
  • 86