Convert from List into IEnumerable format

What'sUP picture What'sUP · Jan 15, 2011 · Viewed 239.6k times · Source
IEnumerable<Book> _Book_IE
List<Book> _Book_List

How shall I do in order to convert _Book_List into IEnumerable format?

Answer

Darin Dimitrov picture Darin Dimitrov · Jan 15, 2011

You don't need to convert it. List<T> implements the IEnumerable<T> interface so it is already an enumerable.

This means that it is perfectly fine to have the following:

public IEnumerable<Book> GetBooks()
{
    List<Book> books = FetchEmFromSomewhere();    
    return books;
}

as well as:

public void ProcessBooks(IEnumerable<Book> books)
{
    // do something with those books
}

which could be invoked:

List<Book> books = FetchEmFromSomewhere();    
ProcessBooks(books);