NameValueCollection - ToDictionary
A NameValueCollection extension method that converts the @this to a dictionary.
public static void Main() { var input = new NameValueCollection {{"txt", "notepad.exe"}, {"bmp", "paint.exe"}, {"dib", "paint.exe"}, {"rtf", "wordpad.exe"}}; // C# Extension Method: NameValueCollection - ToDictionary var output = input.ToDictionary(); // return a Dictionary; foreach (var item in output) { Console.WriteLine("[{0}, {1}]", item.Key, item.Value); } }
View Source
using System.Collections.Generic; using System.Collections.Specialized; public static partial class Extensions { /// <summary> /// A NameValueCollection extension method that converts the @this to a dictionary. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>@this as an IDictionary<string,object></returns> public static IDictionary<string, object> ToDictionary(this NameValueCollection @this) { var dict = new Dictionary<string, object>(); if (@this != null) { foreach (string key in @this.AllKeys) { dict.Add(key, @this[key]); } } return dict; } }