C# Generics : List<Object> or new class extends List <Object>

c#, class, generics

Solution

There is a small overhead in having an extra type (`ProductList`), but nothing huge. But it really depends on what you want to do. In many ways, `ProductList` is still a bad idea since it burns `List<T>` into the public API, and `List<T>` isn't very extensible (none of the methods are `virtual`, for example). `Collection<T>` might have more extensibility options.

I'd argue in favor of abstraction or encapsulation:

public static IList<Product> GetProductList() {...} // abstraction; can return
                                                    // List<Product> if we want

or:

public class ProductList : IList<Product> {...} // encapsulation, but need to
                                                // add a lot of dull code

It is a shame that C# doesn't make the encapsulation approach simple (I'm thinking "mixins").

Note that another trick (sometimes suitable, sometimes not) would be to use extension methods to add the illusion of extra methods on `IList<Product>`... this is a tricky debate, so I'm just mentioning it, not saying "do this".

Problem

I have a class called Product in my Business object and in another class i want to return a list of objects of this class.Which approach i should use ? ``` public static List<Product> GetProductList() { .... } ``` or create another class in my Business object namspace called ProductList which extends `List <Products>` as follows: ``` public class ProductList :List<Products > { .... } ``` and use it there ``` public static ProductList GetProductList() { .... } ``` Is there any difference between these two ? How abt the memory allocation and performance ?

Original source