Arraylist versus List

Asked

Viewed 785 times

9

Why in C# we should prefer to use the List instead of ArrayList?

2 answers

11


Because List is generic and ArrayList is not. The data type of ArrayList is a object, Which means you can put anything in it. In static languages this is not suitable, you lose the security of types, can no longer rely on the contents of the list. Already List<T> determines what type you can put in it, so all the code is reliable that will only have elements of that specific type and types derived from it that are obviously compatible with this type. The compiler itself can ensure type security.

It is not only safety, it is performance as well. Thus the casting and the Boxing (types per value can only be put by reference on a ArrayList, this is terrible.

The ArrayList exists because C# 1 did not have generics yet. The solution when I needed type security was to create a new structure similar to the ArrayList, but that internally it had a specific type, so it would give the safety and performance, but to make one for each type it needed was practically impossible.

It is still possible to use List<object> if you want to accept any type of object in the list, but rarely this is what is desired. It is also possible to use List<dynamic> where the compiler neither checks the access to the object, which has advantages and disadvantages. When using the List<object> every time you pick up the element you can only access members of the type Object, unless you make a cast for the type you want. It takes work, the code gets messy and verbose, is slow, and can easily generate errors if you’re not too careful.

Example:

using System.Collections;
using System.Collections.Generic;

public class Program {
    public static void Main() {
        var array = new ArrayList() { 1, "texto" };
        var total = 0;
        foreach (var item in array) {
            //total += item; //se tirar o comentário dá erro quando tentar somar a string
        }
        //array[1].Contains("t"); //mesmo sendo string não pode acessar Contains() porque o tipo é object
        ((string)array[1]).Contains("t");
        var list = new List<int>() { 1, /*"texto"); //<-- Error at compile process //ddaria erro aqui*/ };
        total = 0;
        foreach (var item in list) total += item;
    }
}

Behold working ideone. And in the .NET Fiddle. Also put on the Github for future reference.

3

List<T> is a generic class. It supports storing values of a specific type without the need to force from or to object.

ArrayList simply stores object references. As a generic collection, List<T> implements the IEnumerable<T> generic interface and can be easily used in LINQ (without needing Cast or OfType).

ArrayList belongs to days when C# did not have generics. It is depreciated in favor of List<T>. You must not use ArrayList in a new code that targets . NET> = 2.0, unless you have to connect to an old API that uses it.

reference: stackoverflow.com/a/2309699/4190610

  • https://stackoverflow.com/a/2309699/4190610

Browser other questions tagged

You are not signed in. Login or sign up in order to post.