Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Converts the specified collection to its string representation

#region License and Copyright /*  * Dotnet Commons Collections   *   *  * This library is free software; you can redistribute it and/or modify it   * under the terms of the GNU Lesser General Public License as published by   * the Free Software Foundation; either version 2.1 of the License, or   * (at your option) any later version.  *  * This library is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY   * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License   * for more details.   *  * You should have received a copy of the GNU Lesser General Public License   * along with this library; if not, write to the   * Free Software Foundation, Inc.,   * 59 Temple Place,   * Suite 330,   * Boston,   * MA 02111-1307   * USA   *   */ #endregion using System; using System.Collections; namespace Dotnet.Commons.Lang {     class MainClass{         /// <summary>         /// Converts the specified collection to its string representation.         /// </summary>         /// <param name="c">The collection to convert to string.</param>         /// <returns>A string representation of the specified collection.</returns>         public static System.String CollectionToString(System.Collections.ICollection c)         {             System.Text.StringBuilder s = new System.Text.StringBuilder();             if (c != null)             {                 System.Collections.ArrayList l = new System.Collections.ArrayList(c);                 bool isDictionary = (c is System.Collections.BitArray || c is System.Collections.Hashtable || c is System.Collections.IDictionary || c is System.Collections.Specialized.NameValueCollection || (l.Count > 0 && l[0] is System.Collections.DictionaryEntry));                 for (int index = 0; index < l.Count; index++)                 {                     if (l[index] == null)                         s.Append("null");                     else if (!isDictionary)                         s.Append(l[index]);                     else                     {                         isDictionary = true;                         if (c is System.Collections.Specialized.NameValueCollection)                             s.Append(((System.Collections.Specialized.NameValueCollection)c).GetKey(index));                         else                             s.Append(((System.Collections.DictionaryEntry)l[index]).Key);                         s.Append("=");                         if (c is System.Collections.Specialized.NameValueCollection)                             s.Append(((System.Collections.Specialized.NameValueCollection)c).GetValues(index)[0]);                         else                             s.Append(((System.Collections.DictionaryEntry)l[index]).Value);                     }                     if (index < l.Count - 1)                         s.Append(", ");                 }                 if (isDictionary)                 {                     if (c is System.Collections.ArrayList)                         isDictionary = false;                 }                 if (isDictionary)                 {                     s.Insert(0, "{");                     s.Append("}");                 }                 else                 {                     s.Insert(0, "[");                     s.Append("]");                 }             }             else                 s.Insert(0, "null");             return s.ToString();         }    } }