StringEnumerator.Reset 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定列舉值至它的初始位置,這是在集合中第一個項目之前。
public:
void Reset();
public void Reset ();
member this.Reset : unit -> unit
Public Sub Reset ()
例外狀況
建立列舉值之後,集合已修改。
範例
下列程式代碼範例示範 的數個屬性和方法 StringEnumerator。
#using <System.dll>
using namespace System;
using namespace System::Collections::Specialized;
int main()
{
// Creates and initializes a StringCollection.
StringCollection^ myCol = gcnew StringCollection;
array<String^>^myArr = {"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.
*/
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.
*/
Imports System.Collections.Specialized
Public Class SamplesStringEnumerator
Public Shared Sub Main()
' Creates and initializes a StringCollection.
Dim myCol As New StringCollection()
Dim myArr() As [String] = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
myCol.AddRange(myArr)
' Enumerates the elements in the StringCollection.
Dim myEnumerator As StringEnumerator = myCol.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine("{0}", myEnumerator.Current)
End While
Console.WriteLine()
' Resets the enumerator and displays the first element again.
myEnumerator.Reset()
If myEnumerator.MoveNext() Then
Console.WriteLine("The first element is {0}.", myEnumerator.Current)
End If
End Sub
End Class
'This code produces the following output.
'
'red
'orange
'yellow
'green
'blue
'indigo
'violet
'
'The first element is red.
備註
Reset 會將列舉值移至集合的開頭,再移至第一個專案之前。 在 之後 Reset, MoveNext 必須先呼叫 ,才能將列舉值前移至集合的第一個專案,才能讀取 的值 Current。