NameValueCollection nvc = new NameValueCollection(); nvc.Add("Key1", "Value1"); nvc.Add("Key2", "Value2, Value3"); Dictionarydict = nvc.ToDictionary(); foreach (KeyValuePair kvp in dict) { Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value); }
NameValueCollection nvc = new NameValueCollection(); nvc.Add("Key1", "Value1"); nvc.Add("Key2", "Value2"); nvc.Add("Key2", "Value3"); DictionaryIn this example, we create a NameValueCollection and add three key-value pairs to it, with the second key having two values. We then call the ToDictionary method to convert this collection into a dictionary. Since a dictionary cannot have duplicate keys, the second key will only have one value, which is the last one that was added. The System.Collections.Specialized NameValueCollection class is part of the .NET Framework Class Library.dict = nvc.ToDictionary(); foreach (KeyValuePair kvp in dict) { Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value); }