How to get IEnumerable <T> from list <T>?
I have a list and I need IEnumerable, however List.GetEnumerator () returns List.Enumerator ...
Is there an easy way to get (casting?) IEnumerator? (I currently solved this with a loop, however, I believe listing an enumerator would be a much better solution) ...
+6
David BoΕΎjak
source share5 answers
A List<T> already IEnumerable<T> .
+15
leppie
source shareI think you will find that the general list implements IEnumerable, so you do not need to do anything.
In what situation are you trying to use this?
+4
Giles smith
source shareIt will implicitly convert, therefore IEnumerable<MyType> foo = myList;
+3
Dave downs
source shareList<T> implements IEnumerable<T> , so you do not need to use it:
public IEnumerable<T> GetIEnumerable() { List<T> yourListOfT = GetList(); return yourListOfT; } +3
GenericTypeTea
source shareThe list implements IEnumerable, so just use your list.
+2
Tokk
source share