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 ?
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,ProductListis still a bad idea since it burnsList<T>into the public API, andList<T>isn’t very extensible (none of the methods arevirtual, for example).Collection<T>might have more extensibility options.I’d argue in favor of abstraction or encapsulation:
or:
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”.