C # When should I use List and when should I use arraylist?

As the title says, when should a List be used and when should an ArrayList be used?

thank

+43
arraylist list c #
Apr 7 '09 at 12:31
source share
8 answers

The main time using ArrayList is in .NET 1.1

Other than that, List<T> completely (for your local T ) ...

For those (rare) cases where you don't know the type up (and can't use generics), even a List<object> more useful than an ArrayList (IMO).

+83
Apr 07 '09 at 12:32
source share

You should always use List<TypeOfChoice> (introduced in .NET 2.0 with generics), since it is TypeSafe and faster than ArrayList (without unnecessary boxing / unpacking).

Only if I could think about where the ArrayList might be useful to you, will you need to interact with old things (.NET 1.1) or you need an array of objects of different types, and you load everything as an object, but you can do the latter with List<Object> , which is usually better.

+12
Apr 07 '09 at 12:35
source share

Since List is a generic class, I would always use List.

ArrayList is a .NET 1.x class (still accessible and valid, though), but it is not "typed" / generic, so you will need to throw the elements from the "object" back into the desired type; whereas when using List you should not do this.

+5
Apr 07 '09 at 12:33
source share

Use List where possible. I do not see any use in ArrayList when a high-performance list exists.

+4
Apr 7 '09 at 12:36
source share

ArrayList is an older .NET data structure. If you are using .NET 2.0 or higher, always use List when an array needs to store elements of the same type. Using List over ArrayList improves performance and usability.

+3
Apr 07 '09 at 12:40
source share

As another said. You should use the general List, almost always when you know the type (C # is a language with a strong type) and other ways when you do polymorphism / inheritance classes or other similar things.

0
Apr 7 '09 at 12:40
source share

If you do not want to use Linq queries, you do not need to use List. If you want to use, you should prefer a list.

0
Dec 12 '11 at 7:30
source share

Generics was introduced in .Net 2.0. If you are using earlier versions of .Net, you can use a list of arrays, which we can use with the general list itself. The list of arrays is outdated and does not provide better type safety, and also creates problems with boxing and unpacking. But the general list will not be.

0
Apr 04 '13 at 3:23
source share



All Articles