Export (0) Print
Expand All

StringCollection Class

Represents a collection of strings.

Namespace: System.Collections.Specialized
Assembly: System (in system.dll)

[SerializableAttribute] 
public ref class StringCollection : IList, ICollection, IEnumerable
/** @attribute SerializableAttribute() */ 
public class StringCollection implements IList, ICollection, 
	IEnumerable
SerializableAttribute 
public class StringCollection implements IList, ICollection, 
	IEnumerable
Not applicable.

StringCollection accepts a null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.

String comparisons are case-sensitive.

Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

The following code example demonstrates several of the properties and methods of StringCollection.

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

void PrintValues1( StringCollection^ myCol );
void PrintValues2( StringCollection^ myCol );
void PrintValues3( StringCollection^ myCol );

int main()
{
   
   // Create and initializes a new StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   
   // Add a range of elements from an array to the end of the StringCollection.
   array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};
   myCol->AddRange( myArr );
   
   // Display the contents of the collection using for each. This is the preferred method.
   Console::WriteLine( "Displays the elements using for each:" );
   PrintValues1( myCol );

   // Display the contents of the collection using the enumerator.
   Console::WriteLine( "Displays the elements using the IEnumerator:" );
   PrintValues2( myCol );
   
   // Display the contents of the collection using the Count and Item properties.
   Console::WriteLine( "Displays the elements using the Count and Item properties:" );
   PrintValues3( myCol );
   
   // Add one element to the end of the StringCollection and insert another at index 3.
   myCol->Add( "* white" );
   myCol->Insert( 3, "* gray" );
   Console::WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" );
   PrintValues1( myCol );
   
   // Remove one element from the StringCollection.
   myCol->Remove( "yellow" );
   Console::WriteLine( "After removing \"yellow\":" );
   PrintValues1( myCol );
   
   // Remove all occurrences of a value from the StringCollection.
   int i = myCol->IndexOf( "RED" );
   while ( i > -1 )
   {
      myCol->RemoveAt( i );
      i = myCol->IndexOf( "RED" );
   }

   
   // Verify that all occurrences of "RED" are gone.
   if ( myCol->Contains( "RED" ) )
      Console::WriteLine( "*** The collection still contains \"RED\"." );

   Console::WriteLine( "After removing all occurrences of \"RED\":" );
   PrintValues1( myCol );
   
   // Copy the collection to a new array starting at index 0.
   array<String^>^myArr2 = gcnew array<String^>(myCol->Count);
   myCol->CopyTo( myArr2, 0 );
   Console::WriteLine( "The new array contains:" );
   for ( i = 0; i < myArr2->Length; i++ )
   {
      Console::WriteLine( "   [{0}] {1}", i, myArr2[ i ] );

   }
   Console::WriteLine();
   
   // Clears the entire collection.
   myCol->Clear();
   Console::WriteLine( "After clearing the collection:" );
   PrintValues1( myCol );
}


// Uses the for each statement which hides the complexity of the enumerator.
// NOTE: The for each statement is the preferred way of enumerating the contents of a collection.
void PrintValues1( StringCollection^ myCol )  {
   for each ( Object^ obj in myCol )
      Console::WriteLine( "   {0}", obj );
   Console::WriteLine();
}

// Uses the enumerator. 
void PrintValues2( StringCollection^ myCol )
{
   StringEnumerator^ myEnumerator = myCol->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( "   {0}", myEnumerator->Current );

   Console::WriteLine();
}


// Uses the Count and Item properties.
void PrintValues3( StringCollection^ myCol )
{
   for ( int i = 0; i < myCol->Count; i++ )
      Console::WriteLine( "   {0}", myCol[ i ] );
   Console::WriteLine();
}

/*
This code produces the following output.

Displays the elements using the IEnumerator:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

Displays the elements using the Count and Item properties:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After adding "* white" to the end and inserting "* gray" at index 3:
   RED
   orange
   yellow
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

After removing "yellow":
   RED
   orange
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

After removing all occurrences of "RED":
   orange
   * gray
   green
   blue
   indigo
   violet
   * white

The new array contains:
   [0] orange
   [1] * gray
   [2] green
   [3] blue
   [4] indigo
   [5] violet
   [6] * white

After clearing the collection:

*/

import System.* ;
import System.Collections.* ;
import System.Collections.Specialized.* ;

public class SamplesStringCollection
{
    public static void main(String[] args)
    {
        // Create and initializes a new StringCollection.
        StringCollection myCol = new StringCollection();

        // Add a range of elements from an array to the end of the
        // StringCollection.
        String myArr[] = new String[] {    "RED", "orange", "yellow", "RED", 
            "green", "blue", "RED", "indigo", "violet", "RED" };

        myCol.AddRange(myArr);

        // Display the contents of the collection using foreach. This is the 
        // preferred method.
        Console.WriteLine("Displays the elements using for:");
        PrintValues1(myCol);

        // Display the contents of the collection using the enumerator.
        Console.WriteLine("Displays the elements using the IEnumerator:");
        PrintValues2(myCol);

        // Display the contents of the collection using the Count and Item 
        // properties.
        Console.WriteLine("Displays the elements using the Count and Item" 
            + " properties:");
        PrintValues3(myCol);

        // Add one element to the end of the StringCollection and insert 
        // another at index 3.
        myCol.Add("* white");
        myCol.Insert(3, "* gray");
        Console.WriteLine("After adding \"* white\" to the end and inserting" 
            + " \"* gray\" at index 3:");
        PrintValues1(myCol);

        // Remove one element from the StringCollection.
        myCol.Remove("yellow");
        Console.WriteLine("After removing \"yellow\":");
        PrintValues1(myCol);

        // Remove all occurrences of a value from the StringCollection.
        int i = myCol.IndexOf("RED");
        while (i > -1) {
            myCol.RemoveAt(i);
            i = myCol.IndexOf("RED");
        }

        // Verify that all occurrences of "RED" are gone.
        if (myCol.Contains("RED")) {
            Console.WriteLine("*** The collection still contains \"RED\".");
        }

        Console.WriteLine("After removing all occurrences of \"RED\":");
        PrintValues1(myCol);

        // Copy the collection to a new array starting at index 0.
        String myArr2[] = new String[myCol.get_Count()];

        myCol.CopyTo(myArr2, 0);
        Console.WriteLine("The new array contains:");
        for (i = 0; i < myArr2.length; i++) {
            Console.WriteLine("   [{0}] {1}", System.Convert.ToString(i), 
                myArr2.get_Item(i));
        }

        Console.WriteLine();

        // Clears the entire collection.
        myCol.Clear();
        Console.WriteLine("After clearing the collection:");
        PrintValues1(myCol);
    } //main

    public static void PrintValues1(StringCollection myCol)
    {
        Object obj = new Object();

        for (int iCtr = 0; iCtr < myCol.get_Count(); iCtr++) {
            obj = myCol.get_Item(iCtr);
            Console.WriteLine("   {0}", obj);
        }

        Console.WriteLine();
    } //PrintValues1

    // Uses the enumerator. 
    public static void PrintValues2(StringCollection myCol)
    {
        StringEnumerator myEnumerator = myCol.GetEnumerator();

        while (myEnumerator.MoveNext()) {
            Console.WriteLine("   {0}", myEnumerator.get_Current());
        }

        Console.WriteLine();
    } //PrintValues2

    // Uses the Count and Item properties.
    public static void PrintValues3(StringCollection myCol)
    {
        for (int i = 0; i < myCol.get_Count(); i++) {
            Console.WriteLine("   {0}", myCol.get_Item(i));
        }

        Console.WriteLine();
    } //PrintValues3
} //SamplesStringCollection
 
/*
This code produces the following output.

Displays the elements using for:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

Displays the elements using the IEnumerator:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

Displays the elements using the Count and Item properties:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After adding "* white" to the end and inserting "* gray" at index 3:
   RED
   orange
   yellow
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

After removing "yellow":
   RED
   orange
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

After removing all occurrences of "RED":
   orange
   * gray
   green
   blue
   indigo
   violet
   * white

The new array contains:
   [0] orange
   [1] * gray
   [2] green
   [3] blue
   [4] indigo
   [5] violet
   [6] * white

After clearing the collection:

*/

System.Object
  System.Collections.Specialized.StringCollection
     System.Configuration.CommaDelimitedStringCollection

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

This implementation does not provide a synchronized (thread safe) wrapper for a StringCollection, but derived classes can create their own synchronized versions of the StringCollection using the SyncRoot property.

Enumerating through a collection is intrinsically not a thread safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft