What is the difference between ArrayList
and List<>
in C#?
Is it only that List<>
has a type while ArrayList
doesn't?
What is the difference between ArrayList
and List<>
in C#?
Is it only that List<>
has a type while ArrayList
doesn't?
It is not only difference. ArrayList members can be accessed via index like ordinary arrays and also ArrayList members can easily sorted in direct and reverse order and two ArrayList can be easily merged, which is not the case with simple List. See more on
http://www.cirvirlab.com/index.php/c-sharp-code-examples/112-c-sharp-arraylist-example.html
To me its all about knowing your data. If I am continuing to expand my code on the basis of efficiency, I would have to choose the List option as a way of deciphering of my data w/o the unnecessary step of always wondering about types, especially 'Custom Types'. If the machine understands the difference and can determine on it's on what type of data I'm actually dealing with then why should I get in the way and waste time going thru the gyrations of 'IF THEN ELSE' determinations? My philosophy is to let the machine work for me instead of me working on the machine? Knowing the unique differences of different object code commands goes a long way in making your code as efficient.
Tom Johnson (One Entry ... One Exit)
ArrayList
is the collections of different types data whereasList<>
is the collection of similar type of its own depedencties.ArrayList
are not type safe whereasList<T>
are type safe. Simple :).Simple Answer is,
ArrayList is Non-Generic
List is Generic
Example:
Please read the Microsoft official document: https://blogs.msdn.microsoft.com/kcwalina/2005/09/23/system-collections-vs-system-collection-generic-and-system-collections-objectmodel/
Note: You should know Generics before understanding the difference: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/