inspite inspite - 4 months ago 14
C# Question

When to use ArrayList over array[] in c#?

I often use an

instead of a 'normal'

I feel as if I am cheating (or being lazy) when I use an
, when is it okay to use an
over an array?

Bob Bob

Arrays are strongly typed, and work well as parameters. If you know the length of your collection and it is fixed, you should use an array.

ArrayLists are not strongly typed, every Insertion or Retrial will need a cast to get back to your original type. If you need a method to take a list of a specific type, ArrayLists fall short because you could pass in an ArrayList containing any type. ArrayLists use a dynamically expanding array internally, so there is also a hit to expand the size of the internal array when it hits its capacity.

What you really want to use is a generic list like List<T>. This has all the advantages of Array and ArrayLists. It is strongly typed and it supports a variable length of items.