In C# there are no cursors like in Java
- Enumeration
- iterator
- ListIterator
Instead of them the IEnumerable and IEnumerator are used. I would like to know if these cursors in Java have any equivalence in C# as well as ListIterator methods such as listiterator.hasprevious().
Thanks.
In C# you have the interface
IEnumerable
that allows you to traverse the array via aIEnumerator
. A class needs to implementIEnumerable
in order to be used in aforeach
.IEnumerator
it is simpler than its Java counterpart.The function
MoveNext
attempts to advance to the next element. If it succeeds, it returnstrue
, otherwisefalse
.The function
Reset
returns to its starting position (before the first element).The property
Current
allows you to read the element at the current position.You could iterate through an array (implement the interface) like so:
To traverse back a
IEnumerable
you can useReverse
theLinq
.Changing the example from before:
Don't forget to add
using System.Linq
to the beginning of the file.If these options do not suit your needs, you can write your own functions and structures.