Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Create a Class Derived from a Generic List (.NET)?

Tags:

c#

.net

generics

Whats is the difference between:

List<MyType> myList;

and

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.

like image 430
Kaz Avatar asked Dec 19 '25 02:12

Kaz


1 Answers

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.

like image 99
MighMoS Avatar answered Dec 20 '25 15:12

MighMoS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!