Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection with Next()

How can I get next element of element to which I have reference in some collection e.g. List<T> ?

I am not talking about any loops, etc. I want get just one next (or previous) element

like image 636
Saint Avatar asked Aug 10 '26 14:08

Saint


1 Answers

A collection does not have the a concept of "current element". Iterators/Enumerators do. And they have a MoveNext() method. And then you can access the current element of that enumerator with Current.

You can call GetEnumerator() on almost all collections to get an enumerator.

One problem with enumerators is you usually can't copy them nor move them backwards. (Unlike C++ where most collections have more powerful iterators)

like image 123
CodesInChaos Avatar answered Aug 13 '26 03:08

CodesInChaos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!