Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Copies the entire StringCollection values to a one-dimensional array of strings

using System; using System.Collections; using System.Collections.Specialized; public class SamplesStringCollection  {    public static void Main()  {       StringCollection myCol = new StringCollection();       String[] myArr = new String[] { "a", "o"};       myCol.AddRange( myArr );       PrintValues( myCol );       String[] myArr2 = new String[myCol.Count];       myCol.CopyTo( myArr2, 0 );       for ( int i = 0; i < myArr2.Length; i++ )  {          Console.WriteLine( "   [{0}] {1}", i, myArr2[i] );       }    }    public static void PrintValues( IEnumerable myCol )  {       foreach ( Object obj in myCol )          Console.WriteLine( "   {0}", obj );       Console.WriteLine();    } }