在c#中,数组列表和List<>有什么区别?

是不是只有List<>有类型而ArrayList没有?


当前回答

如.NET Framework文档中所述

我们不建议你将数组列表类用于new 发展。相反,我们建议您使用通用的List<T> 类。ArrayList类被设计用来保存异构类 对象的集合。然而,它并不总是提供最好的 的性能。相反,我们建议如下: 对于异构对象集合,使用List<Object>(在c#中)或List(of Object)(在Visual Basic中)类型。 对于对象的同构集合,使用List<T>类。

请参见不应使用非泛型集合

其他回答

使用“List”可以防止强制转换错误。它对于避免运行时强制转换错误非常有用。

例子:

在这里(使用ArrayList),您可以编译这段代码,但稍后会看到一个执行错误。

    // Create a new ArrayList


    System.Collections.ArrayList mixedList = new System.Collections.ArrayList();


    // Add some numbers to the list
    mixedList.Add(7);
    mixedList.Add(21);


    // Add some strings to the list
    mixedList.Add("Hello");
    mixedList.Add("This is going to be a problem");




    System.Collections.ArrayList intList = new System.Collections.ArrayList();
    System.Collections.ArrayList strList = new System.Collections.ArrayList();


    foreach (object obj in mixedList)
    {
        if (obj.GetType().Equals(typeof(int)))
        {
            intList.Add(obj);
        }
        else if (obj.GetType().Equals(typeof(string)))
        {
            strList.Add(obj);
        }
        else
        {
            // error.
        }
    }

要添加的另一个区别是线程同步。

ArrayList provides some thread-safety through the Synchronized property, which returns a thread-safe wrapper around the collection. The wrapper works by locking the entire collection on every add or remove operation. Therefore, each thread that is attempting to access the collection must wait for its turn to take the one lock. This is not scalable and can cause significant performance degradation for large collections. List<T> does not provide any thread synchronization; user code must provide all synchronization when items are added or removed on multiple threads concurrently.

更多信息在这里线程同步在.Net框架

简单的答案是,

数组列表是非泛型的

它是一个对象类型,因此您可以在其中存储任何数据类型。 可以在数组列表中存储任意值(值类型或引用类型),例如string、int、employee和object。(注意,) 装箱和开箱将会发生。 不类型安全。 它更老。

列表是通用的

它是类型的类型,因此您可以在运行时指定T。 根据声明,您只能存储类型为T的值(字符串或int或employee或object)。(注意或) 装箱和开箱不会发生。 类型安全。 它更新。

例子:

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

请阅读微软官方文档:https://blogs.msdn.microsoft.com/kcwalina/2005/09/23/system-collections-vs-system-collection-generic-and-system-collections-objectmodel/

注意:在理解它们的区别之前,您应该了解泛型:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

是的,差不多。List<T>是泛型类。它支持存储特定类型的值,而不强制转换为对象(当T是ArrayList情况下的值类型时,这会引起装箱/拆箱开销)。ArrayList仅存储对象引用。作为一个泛型集合,List<T>实现了泛型IEnumerable<T>接口,可以很容易地在LINQ中使用(不需要任何Cast或OfType调用)。

ArrayList属于c#没有泛型的时代。不赞成使用List<T>。你不应该在以。net >= 2.0为目标的新代码中使用ArrayList,除非你必须与使用它的旧API进行交互。

如.NET Framework文档中所述

我们不建议你将数组列表类用于new 发展。相反,我们建议您使用通用的List<T> 类。ArrayList类被设计用来保存异构类 对象的集合。然而,它并不总是提供最好的 的性能。相反,我们建议如下: 对于异构对象集合,使用List<Object>(在c#中)或List(of Object)(在Visual Basic中)类型。 对于对象的同构集合,使用List<T>类。

请参见不应使用非泛型集合