A Dictionary<string,string> extension method that converts a map to a name value collection. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

A Dictionary<string,string> extension method that converts a map to a name value collection.

Demo Code


using System.Collections.Specialized;
using System.Collections.Generic;

public class Main{
        /// <summary>A Dictionary&lt;string,string&gt; extension method that converts a map to a name value collection.</summary>
        /////w  w  w. j a  va  2  s  . co  m
        /// <param name="map">The map to act on.</param>
        ///
        /// <returns>map as a NameValueCollection.</returns>
        public static NameValueCollection ToNameValueCollection(this Dictionary<string, string> map)
        {
            if (map == null) return new NameValueCollection();

            var nameValues = new NameValueCollection();
            foreach (var item in map)
            {
                nameValues.Add(item.Key, item.Value);
            }
            return nameValues;
        }
}

Related Tutorials