139
IEnumerable<Book> _Book_IE List<Book> _Book_List 

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

4
  • 9
    At least he has a convention, eh? Commented Jan 15, 2011 at 16:14
  • 3
    For people wondering why Kirk's comment have so many "ups", it's because you can't just name your variable the way you want ! there are conventions for that, and it is highly recommended that you follow them, so that your code can be clear and understandable, this will help the people trying to help you in places like Stack-overflow or GitHub! for further information read the book : Clean Code by Robert C. Martin. Commented May 9, 2020 at 7:11
  • 1
    ...and how to go about a naming convention without falling prey to the Smurf Naming anti-pattern? devcards.io/smurf-naming-convention Typically a namespace can provide specificity where needed. Commented May 19, 2020 at 13:17
  • Convertint to ienumerable is specially useful when you need to use the .Reverse() in a Linq expression Commented Sep 8, 2020 at 21:06

7 Answers 7

197

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); 
Sign up to request clarification or add additional context in comments.

2 Comments

If you function returns a Task<IEnumerable<Book>> and you try to return Task.FromResult(list_of_books) this will not work. I used the AsEnumerable extension method to come around this.
@Daniel Are you talking about GetBooks()? You could just change the local variable from List to IEnumerable, couldn't you?
122

You can use the extension method AsEnumerable in Assembly System.Core and System.Linq namespace :

List<Book> list = new List<Book>(); return list.AsEnumerable(); 

This will, as said on this MSDN link change the type of the List in compile-time. This will give you the benefits also to only enumerate your collection we needed (see MSDN example for this).

2 Comments

If you are not suppose to add something to the Collection then you should use/return IEnumerable.
using System.Linq;
18

Why not use a Single liner ...

IEnumerable<Book> _Book_IE= _Book_List as IEnumerable<Book>; 

Comments

11

As far as I know List<T> implements IEnumerable<T>. It means that you do not have to convert or cast anything.

1 Comment

It depends. If you try to set a IEnumerable<IList<obj>> to an IEnumerable<IEnumerable<obj>> it gives a compiler error since the second does not inherit from the first one.
7
IEnumerable<Book> _Book_IE; List<Book> _Book_List; 

If it's the generic variant:

_Book_IE = _Book_List; 

If you want to convert to the non-generic one:

IEnumerable ie = (IEnumerable)_Book_List; 

2 Comments

You don't need the cast here.
You do if you want the specific methods from the non-generic interface, as some of them are explicitly implemented, or am I'm on the wrong track here?
0

You need to

using System.Linq; 

to use IEnumerable options at your List.

Comments

0

I couldn't directly use IEnumerable because it was being used within another generic (function as it happens) and can't use abstract types for this purpose.

So I had to use List.

I was serialising and deserialising with JSON but that had to use IEnumerable for Newtonsoft.Json calls, otherwise it produces garbage. (see JSON.NET DeserializeObject to List of Objects).

But on the return function from the deserialiser, the variable of the IEnumerable can be returned by calling ToList() on the deserialised object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.