How is this dictionary possible <TKey, TValue> exception?
Given the following stack trace:
MESSAGE: Value cannot be null.Parameter name: key SOURCE: mscorlib TARGETSITE: Void ThrowArgumentNullException(System.ExceptionArgument) STACKTRACE: at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) at System.Collections.Generic.Dictionary'2.FindEntry(TKey key) at System.Collections.Generic.Dictionary'2.get_Item(TKey key) at MyCompany.MAF.Agent.ServiceContracts.ConvertUtils.Convert(Dictionary'2 from) in D:\Development\MAF\Agent\MyCompany.MAF.Agent\ServiceContracts\ConvertUtils.cs:line 11 I conclude that somehow the following block of code got zero from the collection of dictionary input keys. However, the input dictionary is an instance of Dictionary<string, string> . The implementation of Dictionary<string, string> makes this condition impossible. When adding an item with a zero key, an exception is thrown.
internal static KeyValuePair<string, string>[] Convert(IDictionary<string, string> from) { List<KeyValuePair<string, string>> ret = new List<KeyValuePair<string, string>>(); foreach (string key in from.Keys) ret.Add(new KeyValuePair<string, string>(key, from[key])); return ret.ToArray(); } I often had this problem because I made a mistake allowing multiple threads to access the same dictionary. Make sure this is not the case, because Dictionary not thread safe.
(By the way, your method can be greatly simplified. Dictionary<K,V> already has IEnumerable<KeyValuePair<K,V>> . You can just do ToArray on one.
It looks like your IDictionary argument has an element with the Key parameter, which is null.
Parameter validation for IDictionary is likely to occur somewhere inside the structure.
This exception occurs if the dictionary key is null . Since the built-in Dictionary class does not allow such keys, you can use your own IDictionary -patent class, which allows null .
Not sure about zero, but why aren't you using:
internal static KeyValuePair<string, string>[] Convert(IDictionary<string, string> from) { return from.ToArray(); } Edit: regarding null values. Do you have multiple threads accessing this IDictionary? Corruption is possible if you are not thread safe. See this post for an example of corruption in
Queue<T> class. How to change the collection of a queue in a loop?
Is it possible that another thread affects the conversion of dirctionary to convert?
Threading may well be the cause, but in our case it was a red herring. You may also encounter this problem if you have a release build where the function has been built in. Consider this reprogramming:
class Program { static string someKey = null; static void Main(string[] args) { try { object thing; if (SomeSeeminglyUnrelatedFunction(someKey, out thing)) Console.WriteLine("Found"); things.TryGetValue(someKey, out thing); } catch (Exception ex) { Console.WriteLine(ex.StackTrace.ToString()); throw; } Console.ReadKey(); } private static Dictionary<string, object> stuff = new Dictionary<string, object>(); private static Dictionary<string, object> things = new Dictionary<string, object>(); private static bool SomeSeeminglyUnrelatedFunction(string key, out object item) { return stuff.TryGetValue(key, out item); } } On my machine, this gives the following debug stack trace:
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key) at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value) at MyApp.Program.SomeSeeminglyUnrelatedFunction(String key, Object& item) at MyApp.Program.Main(String[] args) But this gives a stack trace in Release build :
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key) at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value) at MyApp.Program.Main(String[] args) You will be tempted to think with this last stack trace that it should be things.TryGetValue , which throws an exception when it was actually a SomeSeeminglyUnrelatedFunction function that was built into Main .
So, if you land here, think about whether you have a similar problem. (I understand that this may not be a direct answer to the OP question, but he would like to share something that can help the future visitor.)