ArrayList vs List<> in C#

后端 未结 12 1971
深忆病人
深忆病人 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 05:02

    Yes, pretty much. List 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 implements the generic IEnumerable 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. 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.

提交回复
热议问题