IDictionary To String - CSharp System.Collections

CSharp examples for System.Collections:IDictionary

Description

IDictionary To String

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;/*from   w  w  w  . j a  va 2 s .c o m*/

public class Main{
        internal static string ToString<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
        {
            bool firstItem = true;

            if (dictionary == null)
                return "null";

            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            builder.Append("{");

            // Call ToString on each item and put it in.
            foreach (KeyValuePair<TKey, TValue> pair in dictionary) {
                if (!firstItem)
                    builder.Append(", ");

                if (pair.Key == null)
                    builder.Append("null");
               
                else
                    builder.Append(pair.Key.ToString());

                builder.Append("->");

                if (pair.Value == null)
                    builder.Append("null");
               
                else
                    builder.Append(pair.Value.ToString());


                firstItem = false;
            }

            builder.Append("}");
            return builder.ToString();
        }
        #region String representations

        internal static string ToString<T>(IEnumerable<T> collection)
        {
            return ToString(collection);
        }
}

Related Tutorials