Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Iterator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===.NET=== Iterators in the [[.NET Framework]] (i.e. C#) are called "enumerators" and represented by the <code>IEnumerator</code> interface.<ref name=Albahari>{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |date=2022 |publisher= O'Reilly |isbn= 978-1-098-12195-2}}</ref>{{rp|189β190, 344}}<ref name=Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}</ref>{{rp|53β54}}<code>IEnumerator</code> provides a <code>MoveNext()</code> method, which advances to the next element and indicates whether the end of the collection has been reached;<ref name=Albahari/>{{rp|344}}<ref name=Skeet/>{{rp|55β56}}<ref name=Price>{{cite book |last=Price | first=Mark J. |title=C# 8.0 and .NET Core 3.0 β Modern Cross-Platform Development: Build Applications with C#, .NET Core, Entity Framework Core, ASP.NET Core, and ML.NET Using Visual Studio Code | publisher= Packt |isbn= 978-1-098-12195-2}}</ref>{{rp|89}} a <code>Current</code> property, to obtain the value of the element currently being pointed at.<ref name=Albahari/>{{rp|344}}<ref name=Skeet/>{{rp|56}}<ref name=Price/>{{rp|89}} and an optional <code>Reset()</code> method,<ref name=Albahari/>{{rp|344}} to rewind the enumerator back to its initial position. The enumerator initially points to a special value before the first element, so a call to <code>MoveNext()</code> is required to begin iterating. Enumerators are typically obtained by calling the <code>GetEnumerator()</code> method of an object implementing the <code>IEnumerable</code> interface.<ref name=Skeet/>{{rp|54β56}}<ref name=Price/>{{rp|54β56}} a <code>Current</code> property, to obtain the value of the element currently being pointed at;<ref name=Albahari/>{{rp|344}}<ref name=Skeet/>{{rp|56}}<ref name=Price/>{{rp|89}}Container classes typically implement this interface. However, the [[foreach]] statement in [[C Sharp (programming language)|C#]] can operate on any object providing such a method, even if it does not implement <code>IEnumerable</code> ([[duck typing]]).<ref name=Price/>{{rp|89}} Both interfaces were expanded into [[generic programming|generic]] versions in [[.NET Framework#.NET Framework 2.0|.NET 2.0]]. The following shows a simple use of iterators in C# 2.0: <syntaxhighlight lang="csharp"> // explicit version IEnumerator<MyType> iter = list.GetEnumerator(); while (iter.MoveNext()) Console.WriteLine(iter.Current); // implicit version foreach (MyType value in list) Console.WriteLine(value); </syntaxhighlight> C# 2.0 also supports [[#Generators|generators]]: a method that is declared as returning <code>IEnumerator</code> (or <code>IEnumerable</code>), but uses the "<code>yield return</code>" statement to produce a sequence of elements instead of returning an object instance, will be transformed by the compiler into a new class implementing the appropriate interface.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)