Collection.GetEnumerator Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns an enumerator that iterates through the collection.
public:
System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
member this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator
An enumerator that can be used to iterate through the collection.
The following example shows how to use GetEnumerator
to retrieve all the elements of a Collection
object.
Dim customers As New Collection
' Insert code to add elements to the customers collection.
Dim custEnum As IEnumerator = customers.GetEnumerator()
custEnum.Reset()
Dim thisCustomer As Object
While custEnum.MoveNext()
thisCustomer = custEnum.Current()
' Insert code to process this element of the collection.
End While
GetEnumerator
constructs and returns an enumerator object, which implements the IEnumerator interface of the System.Collections namespace. The enumerator object exposes the Current property and the MoveNext and Reset methods. For more information, see For Each...Next Statement.
The For Each...Next Statement calls GetEnumerator
to obtain an enumerator object to support iteration over a collection's elements. Normally, you use a For Each
...Next
loop to traverse a collection or array, and you do not need to call GetEnumerator
explicitly.
If you need closer control over the iteration than the For Each
...Next
statements provide, you can use the GetEnumerator
method to perform a customized traversal. The following are some cases in which you might need to do this.
You might want to return to the beginning of the collection and start the iteration again before it is finished.
You might want to skip over one or more elements for a variety of reasons.
You might need to change the elements of the collection in the middle of a traversal. In this case you must obtain a new enumerator object because the previous one is invalidated.
Продукт | Версії |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Відгук про .NET
.NET – це проект із відкритим кодом. Виберіть посилання, щоб надати відгук: