1

I realize that if you are running .NET 1.1, then generics aren't available. But, if you are using .NET 2.0+, then are there any advantages to using the ArrayList class over List<object>?

Basically, as long as you declare the type T as System.Object, then why not use the List<T> class instead?

myermian
  • 31,823
  • 24
  • 123
  • 215

3 Answers3

4

I would not use it for anything new, and I make it a point to factor it out of existing code where it is convenient to do so. List eliminates much unnecessary type casting, improving performance while reducing the potential for run-time errors. It is also so prevalent now that most extension methods and libraries will be built for it, rather than ArrayList, making it a better choice even if it were technically on par with ArrayList, which it is not.

Aside from old legacy code, I do not believe .NET would suffer at all if it simply vanished from the library.

Daniel
  • 582
  • 8
  • 15
3

I agree with Daniel.

In addition, however, there is specific performance information from Microsoft on MSDN comparing List<T> and ArrayList that is worth a read.

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

In deciding whether to use the List or ArrayList class, both of which have similar functionality, remember that the List class performs better in most cases and is type safe. If a reference type is used for type T of the List class, the behavior of the two classes is identical. However, if a value type is used for type T, you need to consider implementation and boxing issues.

If a value type is used for type T, the compiler generates an implementation of the List class specifically for that value type. That means a list element of a List object does not have to be boxed before the element can be used, and after about 500 list elements are created the memory saved not boxing list elements is greater than the memory used to generate the class implementation.

Make certain the value type used for type T implements the IEquatable generic interface. If not, methods such as Contains must call the Object.Equals(Object) method, which boxes the affected list element. If the value type implements the IComparable interface and you own the source code, also implement the IComparable generic interface to prevent the BinarySearch and Sort methods from boxing list elements. If you do not own the source code, pass an IComparer object to the BinarySearch and Sort methods

It is to your advantage to use the type-specific implementation of the List class instead of using the ArrayList class or writing a strongly typed wrapper collection yourself. The reason is your implementation must do what the .NET Framework does for you already, and the common language runtime can share Microsoft intermediate language code and metadata, which your implementation cannot.

Inisheer
  • 20,376
  • 9
  • 50
  • 82
0

List is a much better option where type casting becomes a bigger issue in complex applications

Nisha
  • 1,379
  • 16
  • 28