Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Reset Method
Collapse the table of content
Expand the table of content

StringEnumerator.Reset Method

Sets the enumerator to its initial position, which is before the first element in the collection.

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

public void Reset()

ExceptionCondition
InvalidOperationException

The collection was modified after the enumerator was created.

Reset moves the enumerator to the beginning of the collection, before the first element. After Reset, MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of Current.

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

using System;
using System.Collections.Specialized;

public class SamplesStringEnumerator  {

   public static void Main()  {

      // Creates and initializes a StringCollection.
      StringCollection myCol = new StringCollection();
      String[] myArr = new String[] { "red", "orange", "yellow", "green", "blue", "indigo", "violet" };
      myCol.AddRange( myArr );

      // Enumerates the elements in the StringCollection.
      StringEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "{0}", myEnumerator.Current );
      Console.WriteLine();

      // Resets the enumerator and displays the first element again.
      myEnumerator.Reset();
      if ( myEnumerator.MoveNext() )
         Console.WriteLine( "The first element is {0}.", myEnumerator.Current );

   }

}

/*
This code produces the following output.

red
orange
yellow
green
blue
indigo
violet

The first element is red.

*/

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft