Export (0) Print
Expand All

StringCollection.Clear Method

Removes all the strings from the StringCollection.

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

public void Clear ()
public final void Clear ()
public final function Clear ()
Not applicable.

Count is set to zero, and references to other objects from elements of the collection are also released.

This method is an O(n) operation, where n is Count.

The following code example removes elements from the StringCollection.

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();
      String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
      myCol.AddRange( myArr );

      Console.WriteLine( "Initial contents of the StringCollection:" );
      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" );
      }

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

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

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

   }

   public static void PrintValues( IEnumerable myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }

}

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After removing "yellow":
   RED
   orange
   RED
   green
   blue
   RED
   indigo
   violet
   RED

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

After clearing the collection:

*/

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

public class SamplesStringCollection
{   
    public static void main(String[] args)
    {            
        // Creates and initializes a new StringCollection.
        StringCollection myCol =  new StringCollection();
        String myArr[] = new String[]{"RED", "orange", "yellow", "RED",
            "green"    , "blue", "RED", "indigo", "violet", "RED"};
        myCol.AddRange(myArr);
        
        Console.WriteLine("Initial contents of the StringCollection:");
        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");
        }
        
        Console.WriteLine("After removing all occurrences of \"RED\":");
        PrintValues(myCol);
        
        // Clears the entire collection.
        myCol.Clear();
        
        Console.WriteLine("After clearing the collection:");
        PrintValues(myCol);
    } //main

    public static void PrintValues(IEnumerable myCol) 
    {
        Object obj = null;
        IEnumerator objEnum = myCol.GetEnumerator();

        while (objEnum.MoveNext()) {        
            obj = objEnum.get_Current();
            Console.WriteLine("   {0}", obj);
        }

        Console.WriteLine();
    } //PrintValues
} //SamplesStringCollection 

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After removing "yellow":
   RED
   orange
   RED
   green
   blue
   RED
   indigo
   violet
   RED

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

After clearing the collection:

*/

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