foreach with generic list class

aronmatthew

Active member
Joined
Aug 5, 2019
Messages
43
Programming Experience
Beginner
Does anyone have a List Enumerator example for generic list class. One that inherits from IEnumerator. Such that foreach can be used for generic list class.
 
There should be no need. If you're defining your own collection class then it should inherit Collection<T> and you get that functionality for free. Is there any reason that you can't do that?
 
If you look at the documentation, you'll see that List<T> implements IEnumerable<T>:

And then if you refer back to the C# foreach keyword documentation, you'll see that the keyword operates on any IEnumerable<T>:
 
as is I'm simply using a field List<T>.

Then a foreach loop will already work, so what problem are you actually trying to solve? It's not clear that there is one. What exactly is it that you want to do that you are unable to?
 
Back
Top Bottom