How to get IEnumerable<T> from List<T>?
I have a List and I need an IEnumerable, however List.GetEnumerator() returns List.Enumerator ...
Is there a simple way o开发者_如何学Gof 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)...
A List<T>
is already an IEnumerable<T>
.
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?
List<T>
implements IEnumerable<T>
, so you don't need to cast it:
public IEnumerable<T> GetIEnumerable()
{
List<T> yourListOfT = GetList();
return yourListOfT;
}
It'll implicitly convert, so IEnumerable<MyType> foo = myList;
A List implements IEnumerable so just use your List.
精彩评论