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
generics c #
source share
5 answers

A List<T> already IEnumerable<T> .

+15
source share

I 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
source share

It will implicitly convert, therefore IEnumerable<MyType> foo = myList;

+3
source share

List<T> implements IEnumerable<T> , so you do not need to use it:

 public IEnumerable<T> GetIEnumerable() { List<T> yourListOfT = GetList(); return yourListOfT; } 
+3
source share

The list implements IEnumerable, so just use your list.

+2
source share

All Articles