ArrayList vs List<> in C#

后端 未结 12 1965
深忆病人
深忆病人 2020-11-22 04:10

What is the difference between ArrayList and List<> in C#?

Is it only that List<> has a type while ArrayLis

相关标签:
12条回答
  • 2020-11-22 04:49

    ArrayList is the collections of different types data whereas List<> is the collection of similar type of its own depedencties.

    0 讨论(0)
  • 2020-11-22 04:55

    Simple Answer is,

    ArrayList is Non-Generic

    • It is an Object Type, so you can store any data type into it.
    • You can store any values (value type or reference type) such string, int, employee and object in the ArrayList. (Note and)
    • Boxing and Unboxing will happen.
    • Not type safe.
    • It is older.

    List is Generic

    • It is a Type of Type, so you can specify the T on run-time.
    • You can store an only value of Type T (string or int or employee or object) based on the declaration. (Note or)
    • Boxing and Unboxing will not happen.
    • Type safe.
    • It is newer.

    Example:

    ArrayList arrayList = new ArrayList();
    List<int> list = new List<int>();
    
    arrayList.Add(1);
    arrayList.Add("String");
    arrayList.Add(new object());
    
    
    list.Add(1);
    list.Add("String");                 // Compile-time Error
    list.Add(new object());             // Compile-time Error
    

    Please read the Microsoft official document: https://blogs.msdn.microsoft.com/kcwalina/2005/09/23/system-collections-vs-system-collection-generic-and-system-collections-objectmodel/

    Note: You should know Generics before understanding the difference: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    0 讨论(0)
  • 2020-11-22 04:57

    I think, the differences between ArrayList and List<T> are:

    1. List<T>, where T is value-type is faster than ArrayList. This is because List<T> avoids boxing/unboxing (where T is value-type).
    2. Many sources say - usually ArrayList used just for backward compatibility. (is not a real difference, but i think it is important note).
    3. Reflection is easier with nongeneric ArrayList then List<T>
    4. ArrayList has IsSynchronized property. So, It is easy to create and use syncronised ArrayList. I didin't found IsSynchronized property for List<T>. Also Keep in mind this type of synchronization is relatively inefficient, msdn):

      var arraylist = new ArrayList();
      var arrayListSyncronized = ArrayList.Synchronized(arraylist
      Console.WriteLine($"syncronized {arraylist.IsSynchronized}");
      Console.WriteLine($"syncronized {arrayListSyncronized.IsSynchronized}");
      
      var list = new List<object>();
      var listSyncronized = ArrayList.Synchronized(list);
      Console.WriteLine($"syncronized {list.IsSynchronized}");//error, no such prop
      Console.WriteLine($"syncronized {list.IsSynchronized}");//error, no such prop
      
    5. ArrayList has ArrayList.SyncRoot property which can be used for syncronisation (msdn). List<T> hasn't SyncRoot property, so in the following construction you need to use some object if you use List<T>:

      ArrayList myCollection = new ArrayList();
      lock(myCollection.SyncRoot) //  ofcourse you can use another object for this goal
      {
          foreach (object item in myCollection)
          {
              // ...
          }
      }
      
    0 讨论(0)
  • 2020-11-22 04:58

    As mentioned in .NET Framework documentation

    We don't recommend that you use the ArrayList class for new development. Instead, we recommend that you use the generic List<T> class. The ArrayList class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance. Instead, we recommend the following:

    • For a heterogeneous collection of objects, use the List<Object> (in C#) or List(Of Object) (in Visual Basic) type.
    • For a homogeneous collection of objects, use the List<T> class.

    See also Non-generic collections shouldn't be used

    0 讨论(0)
  • 2020-11-22 05:02

    Yes, pretty much. List<T> is a generic class. It supports storing values of a specific type without casting to or from object (which would have incurred boxing/unboxing overhead when T is a value type in the ArrayList case). ArrayList simply stores object references. As a generic collection, List<T> implements the generic IEnumerable<T> interface and can be used easily in LINQ (without requiring any Cast or OfType call).

    ArrayList belongs to the days that C# didn't have generics. It's deprecated in favor of List<T>. You shouldn't use ArrayList in new code that targets .NET >= 2.0 unless you have to interface with an old API that uses it.

    0 讨论(0)
  • 2020-11-22 05:02

    ArrayList are not type safe whereas List<T> are type safe. Simple :).

    0 讨论(0)
提交回复
热议问题