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 ?
The objective of generics was, among other things, to promote code reuse. Your first approach is more than appropriate.
The only times I've ever taken the second approach was when I had to add interface implementations to the collection (such as IDisposable), add extra functionality, or have had to serialize the collection to xaml.
Microsoft Code Analysis recommends deriving from Collection<T> rather than List<T>.
This blog post explains why.
You might derive your own collection class from Collection<T> for the following reasons:
To add additional custom functionality
To expose a COMVisible strongly-typed collection class.
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 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".If you plan to add functionality to the list, such as more convenient lookup methods or product-specific queries, then return your own
ProductList
class.If not, there's no need to wrap it with your own class. Instead you should return an
IList<Product>
orIEnumerable<Product>
, the more generic the better. This keeps the idea of returning a list or a collection of products but doesn't couple you to the actual collection implementation (a linked list, array, whatever).Personally I would go with your first suggestion simply because it means you aren't creating a new class. I do it like this as it prevents having loads of special purpose classes when one generic class would suffice.
public static IList<Product> GetProductList() { .... }
If you just want to return the sequence of Products and would never use any functions on the List, I'd suggest you to use something like: