Should I use ToList?

Should I use ToList?

Unless you simply need an array to meet other constraints you should use ToList . In the majority of scenarios ToArray will allocate more memory than ToList . Both use arrays for storage, but ToList has a more flexible constraint. It needs the array to be at least as large as the number of elements in the collection.

Should you convert IEnumerable to List?

In terms of performance, the answer is it depends. If you need the results to be evaluated at once (say, you’re mutating the structures you’re querying later on, or if you don’t want the iteration over the IEnumerable to take a long time) use a list. Else use an IEnumerable .

Which is faster IEnumerable or list?

IQueryable : IQueryable fetches the Record based on filter wise. IQueryable is faster than IEnumerable. In addition to Munesh Sharma’s answer:IEnumerable loads data in-memory and then apply filters to it one by one but IQueryable apply filters all at once and return the result.

Is IQueryable faster than List?

GetList(context) might return an object backed by a custom LINQ provider (like an Entity Framework collection), then you probably want to leave the data cast as an IQueryable: even though your benchmark shows it being 20 times faster to use a list, that difference is so small that no user is ever going to be able to …

When to use return type like IEnumerable < T >?

If you had a method with an unspecific name like GetResults, then a return type like IList or IEnumerable would seem appropriate to me. If you look at the implementation of the Lookup class with reflector, you’ll see a lot of internal members, that are only accessible to LINQ itself.

Why do I use IList < T > instead of IEnumerable?

It is the very purpose of this method to convert an unspecific IEnumerable to the specific type List . If you had a method with an unspecific name like GetResults, then a return type like IList or IEnumerable would seem appropriate to me.

When to call tolist before returning to IEnumerable?

This can be “remedied” by GetRecordIds calling ToList before returning. The “correct” use here simply depends on what is expected of the class (“live” results, or time of call results).

Do you have to return an IEnumerable to the caller?

Return an IEnumerable. You shouldn’t care about what the caller does with it, because the return type clearly states what the returned value is capable of doing.