Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to use inheritance instead of name aliasing in c#?

In other words, is it correct to use:

public class CustomerList : System.Collections.Generic.List<Customer>
{
    /// supposed to be empty
}

instead of:

using CustomerList = System.Collections.Generic.List<Customer>

I'd rather use the first approach because I'd just define CustomerList once, and every time I needed a customer list I'd always use the same type. On the other hand, using the name aliasing approach not only forces me to have to redefine it everywhere, but also a different alias could be given every time someone wanted to use it (think of a big team), and thus cause the code to be less readable.

Please note that the intention in this case would never be to extend the class, just to create an alias.

like image 913
Trap Avatar asked Sep 29 '08 00:09

Trap


2 Answers

well, unless you are adding some functionality to the base class there is no point in creating a wrapper object. I would go with number two if you really need to, but why not just create a variable?

List<Customer> customerList = new List<Customer>();
like image 110
Ed S. Avatar answered Nov 15 '22 06:11

Ed S.


Don't do it. When people read:

List<Customer> 

they immediately understand it. When they read:

CustomerList

they have to go and figure out what a CustomerList is, and that makes your code harder to read. Unless you are the only one working on your codebase, writing readable code is a good idea.

like image 27
Hallgrim Avatar answered Nov 15 '22 06:11

Hallgrim