i have always thought that returning Arrays were better than lists when having a public API but it seems now there are all these functions on lists that are available through LINQ, etc.
Has the best practice changed here for returning collections of primitives or objects?
for example:
Order[] GetOrders();
List<Order> GetOrders();
IEnumerable<Order> GetOrders();
IQueryable<Order> Get Orders();
I think the most commonly used type is
IEnumerable<T>IEnumerable<T>return type you can use theyieldkeyword and this enables delayed enumeration and execution of your method. (A common complaint for example is that the System.IO.Path.GetFiles() does NOT return aIEnumerable<T>but returns an Array which means that when you call the method all items need to be enumerated regardless if you need them or not. – You have the same disadvantage withList<T>)IEnumerable<T>IEnumerable<T>return type doesn’t assume anything specific about the caller. If the caller needs a list or array they can always create one.IEnumerable<T>is implemented byList<T>andArrayand it is therefore easy to change the implementation of your method and still support the previous return type.