tags:

views:

160

answers:

5

Hello.

I have a List and I need an IEnumerable, however List.GetEnumerator() returns List.Enumerator ...

Is there a simple way of getting (casting to?) an IEnumerator? (currently I have solved this with a loop, however I feel casting the enumerator would be a far better solution)...

+3  A: 

It'll implicitly convert, so IEnumerable<MyType> foo = myList;

Dave
No casting happens, as it is/implements IEnumerable<T>, not castable to.
Dykam
+12  A: 

A List<T> is already an IEnumerable<T>.

leppie
+3  A: 

I think you will find that a generic List implements IEnumerable, so you don't need to do anything.

What situation are you trying to use this in?

Giles
+2  A: 

List<T> implements IEnumerable<T>, so you don't need to cast it:

public IEnumerable<T> GetIEnumerable()
{
    List<T> yourListOfT = GetList();
    return yourListOfT;
}
GenericTypeTea
+1  A: 

A List implements IEnumerable so just use your List.

Tokk