StringEnumerator.MoveNext 方法
定义
将枚举数推进到集合的下一个元素。Advances the enumerator to the next element of the collection.
public:
bool MoveNext();
public bool MoveNext ();
member this.MoveNext : unit -> bool
Public Function MoveNext () As Boolean
返回
如果枚举数已成功地推进到下一个元素,则为 true;如果枚举数传递到集合的末尾,则为 false。true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
例外
集合在枚举器创建后被修改。The collection was modified after the enumerator was created.
示例
下面的代码示例演示了的多个属性和方法 StringEnumerator 。The following code example demonstrates several of the properties and methods of 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 调用之后,枚举数将定位在集合的第一个元素之前,第一次调用将 MoveNext 枚举数移到集合的第一个元素上。After an enumerator is created or after a Reset is called, an enumerator is positioned before the first element of the collection, and the first call to MoveNext moves the enumerator over the first element of the collection.
如果 MoveNext 越过集合的末尾,则枚举器将定位在集合中最后一个元素之后,并 MoveNext 返回 false 。If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. 当枚举器位于此位置时,对的后续调用 MoveNext 也将返回, false 直到 Reset 调用。When the enumerator is at this position, subsequent calls to MoveNext also return false until Reset is called.
只要集合保持不变,枚举器就仍有效。An enumerator remains valid as long as the collection remains unchanged. 如果对集合所做的更改(如添加、修改或删除元素),则枚举器将失效且不可恢复,并且对或的下一个调用将 MoveNext Reset 引发 InvalidOperationException 。If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException. 如果在和之间修改集合 MoveNext Current ,则 Current 将返回它设置为的元素,即使枚举器已失效也是如此。If the collection is modified between MoveNext and Current, Current returns the element that it is set to, even if the enumerator is already invalidated.