Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The given key was not present in the dictionary. Which key?

Is there a way to get the value of the given key in the following exception in C# in a way that affects all generic classes? I think this is a big miss in the exception description from Microsoft.

"The given key was not present in the dictionary." 

A better way would be:

"The given key '" + key.ToString() + "' was not present in the dictionary." 

Solutions might involve mixins or derived classes maybe.

like image 626
Andreas Avatar asked Oct 07 '14 20:10

Andreas


People also ask

How do you solve the given key was not present in the dictionary?

"The given key was not present in the dictionary." A better way would be: "The given key '" + key.

What does the given key was not present in dictionary?

The error 'The given key is not present in the dictionary' simply means that the dictionary does not have a value that corresponds to the key. So, this error would occur in the first line if InputParameters does not contain a key called "Target", or in the second line if there were no "name" in Attributes.

How do you check if a key is in a dictionary C#?

Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the Dictionary. Return Value: This method will return true if the Dictionary contains an element with the specified key otherwise, it returns false.

Can a dictionary have a null key?

Dictionary will hash the key supplie to get the index , in case of null , hash function can not return a valid value that's why it does not support null in key.


2 Answers

This exception is thrown when you try to index to something that isn't there, for example:

Dictionary<String, String> test = new Dictionary<String,String>(); test.Add("Key1","Value1"); string error = test["Key2"]; 

Often times, something like an object will be the key, which undoubtedly makes it harder to get. However, you can always write the following (or even wrap it up in an extension method):

if (test.ContainsKey(myKey))    return test[myKey]; else    throw new Exception(String.Format("Key {0} was not found", myKey)); 

Or more efficient (thanks to @ScottChamberlain)

T retValue; if (test.TryGetValue(myKey, out retValue))     return retValue; else    throw new Exception(String.Format("Key {0} was not found", myKey)); 

Microsoft chose not to do this, probably because it would be useless when used on most objects. Its simple enough to do yourself, so just roll your own!

like image 169
BradleyDotNET Avatar answered Oct 11 '22 23:10

BradleyDotNET


In the general case, the answer is No.

However, you can set the debugger to break at the point where the exception is first thrown. At that time, the key which was not present will be accessible as a value in the call stack.

In Visual Studio, this option is located here:

Debug → Exceptions... → Common Language Runtime Exceptions → System.Collections.Generic

There, you can check the Thrown box.


For more specific instances where information is needed at runtime, provided your code uses IDictionary<TKey, TValue> and not tied directly to Dictionary<TKey, TValue>, you can implement your own dictionary class which provides this behavior.

like image 36
Sam Harwell Avatar answered Oct 11 '22 22:10

Sam Harwell