StringEnumerator.MoveNext 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將列舉值往前推至下集合中的下一個項目。
public:
bool MoveNext();
public bool MoveNext ();
member this.MoveNext : unit -> bool
Public Function MoveNext () As Boolean
傳回
如果列舉值成功前移至下一個項目,則為 true
;如果列舉值超過集合的結尾,則為 false
。
例外狀況
建立列舉值之後,集合已修改。
範例
下列程式代碼範例示範 的數個屬性和方法 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 枚舉器移至集合的第一個專案。
如果 MoveNext 傳遞集合結尾,列舉值會放在集合的最後一個專案後面,並 MoveNext 傳 false
回 。 當列舉值位於這個位置時,後續呼叫 MoveNext 也會傳回 false
,直到呼叫 為止 Reset 。
只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如新增、修改或刪除專案,列舉值就會無法復原,而下一次呼叫 MoveNext 或 Reset 會擲回 InvalidOperationException。 如果在和 Current之間MoveNext修改集合,Current則會傳回它設定為 的專案,即使列舉值已經失效也一樣。