C# – Why Create a Class Derived from a Generic List (.NET)


Whats is the difference between:

List<MyType> myList;


myList : List<MyType>

Its obvious that the first one is list and second one is a class. But my question is what's the advantage of second over first one.

Best Solution

The latter gives you the ability to have a function which takes a myList, instead of just a List. This also means that if the type of myList changes (perhaps to a sorted list) you don't have to change your code anywhere. So instead of declaring List<myType> everwhere, and then having to change them, if you had MyList objects everywhere, you're golden.

Its also a syntactic difference. Does myList have a list, or is it a list?

I would lean towards having a MyList : List<MyType> if it is used commonly throughout your program.