IDictionary To URL Query String - CSharp System.Collections

CSharp examples for System.Collections:IDictionary

Description

IDictionary To URL Query String

Demo Code


using System.Text;
using System.Collections.Generic;

public class Main{

        public static string ToQueryString(IDictionary<string, string> dict)
        {/*from w  w  w .  j av a  2 s. c  o  m*/
            if (dict.Count == 0) 
                return string.Empty;

            var buffer = new StringBuilder();
            var count = 0;
            var end = false;

            foreach (var key in dict.Keys)
            {
                if (count == dict.Count - 1) end = true;

                if (end)
                    buffer.AppendFormat("{0}={1}", key, dict[key]);
                else
                    buffer.AppendFormat("{0}={1}&", key, dict[key]);

                count++;
            }

            return buffer.ToString();
        }
}

Related Tutorials