Scott
Scott

Reputation: 999

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

As the title says when should I use List and when should I use ArrayList?

Thanks

Upvotes: 47

Views: 28500

Answers (8)

kuttychutty
kuttychutty

Reputation: 151

Generics was introduced in .Net 2.0.If you are using earlier versions of .Net ,then you can use the Array List else we can go with the Generic List itself. Array List is the deprecated one and won't provide better type safety and also create boxing and unboxing problems.But Generic List won't.

Upvotes: 0

VIRA
VIRA

Reputation: 1504

If you dont want to use Linq Queries, then you dont need to use List. If you want to use then you must prefer List.

Upvotes: 0

JohnIdol
JohnIdol

Reputation: 50137

You should always use List<TypeOfChoice> (introduced in .NET 2.0 with generics) since it is TypeSafe and faster than ArrayList (no un-necessary boxing/unboxing).

Only case I could think of where an ArrayList could be handy is if you need to interface with old stuff (.NET 1.1) or you need an array of objects of different type and you load up everything as object - but you could do the latter with List<Object> which is generally better.

Upvotes: 14

blitzkriegz
blitzkriegz

Reputation: 9576

ArrayList is an older .NET data structure. If you are using .NET 2.0 or above always use List when the array needs to hold items of the same type. Usage of List over ArrayList improves both performance and usability.

Upvotes: 3

Timotei
Timotei

Reputation: 1929

As other said. You should use the List generic, almost always when you know the type (C# is a strong-typed language), and other ways when u do polymorphic/inhertance classes or other stuff like that.

Upvotes: 0

Niran
Niran

Reputation: 1126

Use List where ever possible. I can't see any use to ArrayList when high performing List exists.

Upvotes: 4

Frederik Gheysels
Frederik Gheysels

Reputation: 56984

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

ArrayList is a .NET 1.x class (still available & valid though), but it is not 'typed'/generic, so you'll need to cast items from 'object' back to the desired type; whereas when using List, you don't have to do that.

Upvotes: 5

Marc Gravell
Marc Gravell

Reputation: 1064254

The main time to use ArrayList is in .NET 1.1

Other than that, List<T> all the way (for your local T)...

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

Upvotes: 90

Related Questions