Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Collections.Generic.IEnumerator: practical reference

System.Collections.Generic.IEnumerator: practical reference: IEnumerator<T> is a stateful cursor advanced by MoveNext, with Current exposing the element at the valid position.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.Collections.Generic.IEnumerator /* select a documented member or constructor */

IEnumerator is a stateful cursor advanced by MoveNext, with Current exposing the element at the valid position.

Normally consume it through foreach; when using it directly, always ensure Dispose is called.

Current has no useful contract before the first successful MoveNext or after MoveNext returns false.

Reset may be unsupported, and modifying the underlying collection usually invalidates the enumerator.

The complete program actually uses System.Collections.Generic.IEnumerator; its exact expected stdout is 8.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

using IEnumerator<int> iterator = new List<int> { 8 }.GetEnumerator();
Console.WriteLine(iterator.MoveNext() ? iterator.Current : -1);

Result

8

The complete program actually uses `System.Collections.Generic.IEnumerator`; its exact expected stdout is `8`.

Sources