Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

StringCollection Class

Represents a collection of strings.

For a list of all members of this type, see StringCollection Members.

System.Object
   System.Collections.Specialized.StringCollection

[Visual Basic]
<Serializable>
Public Class StringCollection
   Implements IList, ICollection, IEnumerable
[C#]
[Serializable]
public class StringCollection : IList, ICollection, IEnumerable
[C++]
[Serializable]
public __gc class StringCollection : public IList, ICollection,
   IEnumerable
[JScript]
public
   Serializable
class StringCollection implements IList, ICollection,
   IEnumerable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. 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 could 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.

Remarks

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

String comparisons are case-sensitive.

Indexes in this collection are zero-based.

Example

[Visual Basic, C#, C++] The following code example demonstrates several of the properties and methods of StringCollection.

[Visual Basic] 
Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesStringCollection   

   Public Shared Sub Main()

      ' Creates and initializes a new StringCollection.
      Dim myCol As New StringCollection()

      Console.WriteLine("Initial contents of the StringCollection:")
      PrintValues(myCol)

      ' Adds a range of elements from an array to the end of the StringCollection.
      Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
      myCol.AddRange(myArr)

      Console.WriteLine("After adding a range of elements:")
      PrintValues(myCol)

      ' Adds one element to the end of the StringCollection and inserts 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:")
      PrintValues(myCol)

      ' Removes one element from the StringCollection.
      myCol.Remove("yellow")

      Console.WriteLine("After removing ""yellow"":")
      PrintValues(myCol)

      ' Removes all occurrences of a value from the StringCollection.
      Dim i As Integer = myCol.IndexOf("RED")
      While i > - 1
         myCol.RemoveAt(i)
         i = myCol.IndexOf("RED")
      End While

      ' Verifies that all occurrences of "RED" are gone.
      If myCol.Contains("RED") Then
         Console.WriteLine("*** The collection still contains ""RED"".")
      End If 
      Console.WriteLine("After removing all occurrences of ""RED"":")
      PrintValues(myCol)

      ' Copies the collection to a new array starting at index 0.
      Dim myArr2(myCol.Count) As [String]
      myCol.CopyTo(myArr2, 0)

      Console.WriteLine("The new array contains:")
      For i = 0 To myArr2.Length - 1
         Console.WriteLine("   [{0}] {1}", i, myArr2(i))
      Next i
      Console.WriteLine()

      ' Clears the entire collection.
      myCol.Clear()

      Console.WriteLine("After clearing the collection:")
      PrintValues(myCol)

   End Sub 'Main

   Public Shared Sub PrintValues(myCol As IEnumerable)
      Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()

      While myEnumerator.MoveNext()
         Console.WriteLine("   {0}", myEnumerator.Current)
      End While
      Console.WriteLine()

   End Sub 'PrintValues

End Class 'SamplesStringCollection 


'This code produces the following output.
'
'Initial contents of the StringCollection:
'
'After adding a range of elements:
'   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:
'


[C#] 
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection  {

   public static void Main()  {

      // Creates and initializes a new StringCollection.
      StringCollection myCol = new StringCollection();

      Console.WriteLine( "Initial contents of the StringCollection:" );
      PrintValues( myCol );

      // Adds 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 );

      Console.WriteLine( "After adding a range of elements:" );
      PrintValues( myCol );

      // Adds one element to the end of the StringCollection and inserts 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:" );
      PrintValues( myCol );

      // Removes one element from the StringCollection.
      myCol.Remove( "yellow" );

      Console.WriteLine( "After removing \"yellow\":" );
      PrintValues( myCol );

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

      // Verifies 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\":" );
      PrintValues( myCol );

      // Copies the collection to a new array starting at index 0.
      String[] myArr2 = new 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:" );
      PrintValues( myCol );

   }

   public static void PrintValues( IEnumerable myCol )  {
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0}", myEnumerator.Current );
      Console.WriteLine();
   }

}

/*
This code produces the following output.

Initial contents of the StringCollection:

After adding a range of elements:
   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:


*/

[C++] 
#using <mscorlib.dll>
#using <System.dll>

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

void PrintValues(IEnumerable* myCol) {
   System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator();
   while (myEnumerator->MoveNext())
      Console::WriteLine(S"   {0}", myEnumerator->Current);
   Console::WriteLine();
}

int main() {

   // Creates and initializes a new StringCollection.
   StringCollection* myCol = new StringCollection();

   Console::WriteLine(S"Initial contents of the StringCollection:");
   PrintValues(myCol);

   // Adds a range of elements from an array to the end of the StringCollection.
   String* myArr[] = { S"RED", S"orange", S"yellow", S"RED", S"green", S"blue", S"RED", S"indigo", S"violet", S"RED" };
   myCol->AddRange(myArr);

   Console::WriteLine(S"After adding a range of elements:");
   PrintValues(myCol);

   // Adds one element to the end of the StringCollection and inserts another at index 3.
   myCol->Add(S"* white");
   myCol->Insert(3, S"* gray");

   Console::WriteLine(S"After adding \"* white\" to the end and inserting \"* gray\" at index 3:");
   PrintValues(myCol);

   // Removes one element from the StringCollection.
   myCol->Remove(S"yellow");

   Console::WriteLine(S"After removing \"yellow\":");
   PrintValues(myCol);

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

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

   Console::WriteLine(S"After removing all occurrences of \"RED\":");
   PrintValues(myCol);

   // Copies the collection to a new array starting at index 0.
   String* myArr2[] = new String*[myCol->Count];
   myCol->CopyTo(myArr2, 0);

   Console::WriteLine(S"The new array contains:");
   for (i = 0; i < myArr2->Length; i++) {
      Console::WriteLine(S"   [{0}] {1}", __box(i), myArr2->Item[i]);
   }
   Console::WriteLine();

   // Clears the entire collection.
   myCol->Clear();

   Console::WriteLine(S"After clearing the collection:");
   PrintValues(myCol);

}

/*
This code produces the following output.

Initial contents of the StringCollection:

After adding a range of elements:
   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:

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Collections.Specialized

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

StringCollection Members | System.Collections.Specialized Namespace | Performing Culture-Insensitive String Operations

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft