Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
266 lines (226 sloc) 18.1 KB
<Type Name="IEnumerator" FullName="System.Collections.IEnumerator">
<TypeSignature Language="C#" Value="public interface IEnumerator" />
<TypeSignature Language="ILAsm" Value=".class public interface auto ansi abstract IEnumerator" />
<TypeSignature Language="DocId" Value="T:System.Collections.IEnumerator" />
<TypeSignature Language="VB.NET" Value="Public Interface IEnumerator" />
<TypeSignature Language="C++ CLI" Value="public interface class IEnumerator" />
<TypeSignature Language="F#" Value="type IEnumerator = interface" />
<AssemblyInfo>
<AssemblyName>System.Runtime</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.0.20.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.2.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Interfaces />
<Attributes>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;xamarinandroid-7.1;xamarinios-10.8;xamarinmac-3.0;netframework-4.8">
<AttributeName>System.Runtime.InteropServices.ComVisible(true)</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-1.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;xamarinandroid-7.1;xamarinios-10.8;xamarinmac-3.0;netframework-4.8">
<AttributeName>System.Runtime.InteropServices.Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")</AttributeName>
</Attribute>
</Attributes>
<Docs>
<summary>Supports a simple iteration over a non-generic collection.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:System.Collections.IEnumerator> is the base interface for all non-generic enumerators. Its generic equivalent is the <xref:System.Collections.Generic.IEnumerator%601?displayProperty=nameWithType> interface.
The `foreach` statement of the C# language (`for each` in Visual Basic) hides the complexity of the enumerators. Therefore, using `foreach` is recommended instead of directly manipulating the enumerator.
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
The <xref:System.Collections.IEnumerator.Reset%2A> method is provided for COM interoperability and does not need to be fully implemented; instead, the implementer can throw a <xref:System.NotSupportedException>.
Initially, the enumerator is positioned before the first element in the collection. You must call the <xref:System.Collections.IEnumerator.MoveNext%2A> method to advance the enumerator to the first element of the collection before reading the value of <xref:System.Collections.IEnumerator.Current%2A>; otherwise, <xref:System.Collections.IEnumerator.Current%2A> is undefined.
<xref:System.Collections.IEnumerator.Current%2A> returns the same object until either <xref:System.Collections.IEnumerator.MoveNext%2A> or <xref:System.Collections.IEnumerator.Reset%2A> is called. <xref:System.Collections.IEnumerator.MoveNext%2A> sets <xref:System.Collections.IEnumerator.Current%2A> to the next element.
If <xref:System.Collections.IEnumerator.MoveNext%2A> passes the end of the collection, the enumerator is positioned after the last element in the collection and <xref:System.Collections.IEnumerator.MoveNext%2A> returns `false`. When the enumerator is at this position, subsequent calls to <xref:System.Collections.IEnumerator.MoveNext%2A> also return `false`. If the last call to <xref:System.Collections.IEnumerator.MoveNext%2A> returned `false`, <xref:System.Collections.IEnumerator.Current%2A> is undefined.
To set <xref:System.Collections.IEnumerator.Current%2A> to the first element of the collection again, you can call <xref:System.Collections.IEnumerator.Reset%2A>, if it's implemented, followed by <xref:System.Collections.IEnumerator.MoveNext%2A>. If <xref:System.Collections.IEnumerator.Reset%2A> is not implemented, you must create a new enumerator instance to return to the first element of the collection.
If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of the enumerator is undefined.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
## Examples
The following code example demonstrates the implementation of the <xref:System.Collections.IEnumerable> and <xref:System.Collections.IEnumerator> interfaces for a custom collection. In this example, members of these interfaces are not explicitly called, but they are implemented to support the use of `foreach` (`for each` in Visual Basic) to iterate through the collection.
[!code-csharp[System.Collections_EnumeratorInterfaces#1](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Collections_EnumeratorInterfaces/cs/ienumerator.cs#1)]
[!code-vb[System.Collections_EnumeratorInterfaces#1](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Collections_EnumeratorInterfaces/vb/ienumerator.vb#1)]
]]></format>
</remarks>
<altmember cref="T:System.Collections.IEnumerable" />
<altmember cref="T:System.Collections.ICollection" />
<altmember cref="T:System.Collections.Generic.IEnumerator`1" />
</Docs>
<Members>
<Member MemberName="Current">
<MemberSignature Language="C#" Value="public object Current { get; }" />
<MemberSignature Language="ILAsm" Value=".property instance object Current" />
<MemberSignature Language="DocId" Value="P:System.Collections.IEnumerator.Current" />
<MemberSignature Language="VB.NET" Value="Public ReadOnly Property Current As Object" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; property System::Object ^ Current { System::Object ^ get(); };" />
<MemberSignature Language="F#" Value="member this.Current : obj" Usage="System.Collections.IEnumerator.Current" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyName>System.Runtime</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.0.20.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.2.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Object</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets the element in the collection at the current position of the enumerator.</summary>
<value>The element in the collection at the current position of the enumerator.</value>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:System.Collections.IEnumerator.Current%2A> is undefined under any of the following conditions:
- The enumerator is positioned before the first element in the collection, immediately after the enumerator is created. <xref:System.Collections.IEnumerator.MoveNext%2A> must be called to advance the enumerator to the first element of the collection before reading the value of <xref:System.Collections.IEnumerator.Current%2A>.
- The last call to <xref:System.Collections.IEnumerator.MoveNext%2A> returned `false`, which indicates the end of the collection.
- The enumerator is invalidated due to changes made in the collection, such as adding, modifying, or deleting elements.
<xref:System.Collections.IEnumerator.Current%2A> returns the same object until <xref:System.Collections.IEnumerator.MoveNext%2A> is called. <xref:System.Collections.IEnumerator.MoveNext%2A> sets <xref:System.Collections.IEnumerator.Current%2A> to the next element.
## Examples
The following code example demonstrates the implementation of the <xref:System.Collections.IEnumerator> interfaces for a custom collection. In this example, <xref:System.Collections.IEnumerator.Current%2A> is not explicitly called, but it is implemented to support the use of `foreach` (`for each` in Visual Basic). This code example is part of a larger example for the <xref:System.Collections.IEnumerator> interface.
[!code-csharp[System.Collections_EnumeratorInterfaces#2](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Collections_EnumeratorInterfaces/cs/ienumerator.cs#2)]
[!code-vb[System.Collections_EnumeratorInterfaces#2](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Collections_EnumeratorInterfaces/vb/ienumerator.vb#2)]
]]></format>
</remarks>
<altmember cref="M:System.Collections.IEnumerator.MoveNext" />
<altmember cref="M:System.Collections.IEnumerator.Reset" />
</Docs>
</Member>
<Member MemberName="MoveNext">
<MemberSignature Language="C#" Value="public bool MoveNext ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance bool MoveNext() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.IEnumerator.MoveNext" />
<MemberSignature Language="VB.NET" Value="Public Function MoveNext () As Boolean" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; bool MoveNext();" />
<MemberSignature Language="F#" Value="abstract member MoveNext : unit -&gt; bool" Usage="iEnumerator.MoveNext " />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Runtime</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.0.20.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.2.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Advances the enumerator to the next element of the collection.</summary>
<returns>
<see langword="true" /> if the enumerator was successfully advanced to the next element; <see langword="false" /> if the enumerator has passed the end of the collection.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
After an enumerator is created or after the <xref:System.Collections.IEnumerator.Reset%2A> method is called, an enumerator is positioned before the first element of the collection, and the first call to the <xref:System.Collections.IEnumerator.MoveNext%2A> method moves the enumerator over the first element of the collection.
If <xref:System.Collections.IEnumerator.MoveNext%2A> passes the end of the collection, the enumerator is positioned after the last element in the collection and <xref:System.Collections.IEnumerator.MoveNext%2A> returns `false`. When the enumerator is at this position, subsequent calls to <xref:System.Collections.IEnumerator.MoveNext%2A> also return `false` until <xref:System.Collections.IEnumerator.Reset%2A> is called.
If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of <xref:System.Collections.IEnumerator.MoveNext%2A> is undefined.
## Examples
The following code example demonstrates the implementation of the <xref:System.Collections.IEnumerator> interfaces for a custom collection. In this example, <xref:System.Collections.IEnumerator.MoveNext%2A> is not explicitly called, but it is implemented to support the use of `foreach` (`for each` in Visual Basic). This code example is part of a larger example for the <xref:System.Collections.IEnumerator> interface.
[!code-csharp[System.Collections_EnumeratorInterfaces#2](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Collections_EnumeratorInterfaces/cs/ienumerator.cs#2)]
[!code-vb[System.Collections_EnumeratorInterfaces#2](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Collections_EnumeratorInterfaces/vb/ienumerator.vb#2)]
]]></format>
</remarks>
<altmember cref="P:System.Collections.IEnumerator.Current" />
<altmember cref="M:System.Collections.IEnumerator.Reset" />
<exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
</Docs>
</Member>
<Member MemberName="Reset">
<MemberSignature Language="C#" Value="public void Reset ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance void Reset() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.IEnumerator.Reset" />
<MemberSignature Language="VB.NET" Value="Public Sub Reset ()" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; void Reset();" />
<MemberSignature Language="F#" Value="abstract member Reset : unit -&gt; unit" Usage="iEnumerator.Reset " />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Runtime</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.0.20.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.2.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Sets the enumerator to its initial position, which is before the first element in the collection.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of <xref:System.Collections.IEnumerator.Reset%2A> is undefined.
The <xref:System.Collections.IEnumerator.Reset%2A> method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a <xref:System.NotSupportedException>.
## Examples
The following code example demonstrates the implementation of the <xref:System.Collections.IEnumerator> interfaces for a custom collection. In this example, <xref:System.Collections.IEnumerator.Reset%2A> is not explicitly called, but it is implemented to support the use of `foreach` (`for each` in Visual Basic). This code example is part of a larger example for the <xref:System.Collections.IEnumerator> interface.
[!code-csharp[System.Collections_EnumeratorInterfaces#2](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Collections_EnumeratorInterfaces/cs/ienumerator.cs#2)]
[!code-vb[System.Collections_EnumeratorInterfaces#2](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Collections_EnumeratorInterfaces/vb/ienumerator.vb#2)]
]]></format>
</remarks>
<block subset="none" type="overrides">
<para>All calls to <see cref="M:System.Collections.IEnumerator.Reset" /> must result in the same state for the enumerator. The preferred implementation is to move the enumerator to the beginning of the collection, before the first element. This invalidates the enumerator if the collection has been modified since the enumerator was created, which is consistent with <see cref="M:System.Collections.IEnumerator.MoveNext" /> and <see cref="P:System.Collections.IEnumerator.Current" />.</para>
</block>
<altmember cref="M:System.Collections.IEnumerator.MoveNext" />
<altmember cref="P:System.Collections.IEnumerator.Current" />
<exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
</Docs>
</Member>
</Members>
</Type>
You can’t perform that action at this time.