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

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


当前回答

我认为,ArrayList和List<T>之间的区别是:

List<T>, where T is value-type is faster than ArrayList. This is because List<T> avoids boxing/unboxing (where T is value-type). Many sources say - usually ArrayList used just for backward compatibility. (is not a real difference, but i think it is important note). Reflection is easier with nongeneric ArrayList then List<T> 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 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) { // ... } }

其他回答

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

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

使用“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不是类型安全的,而List<T>是类型安全的。简单:)。

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

例子:

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

ArrayList array1 = new ArrayList();
array1.Add(1);
array1.Add("Pony"); //No error at compile process
int total = 0;
foreach (int num in array1)
{
 total += num; //-->Runtime Error
}

如果使用List,可以避免以下错误:

List<int> list1 = new List<int>();
list1.Add(1);
//list1.Add("Pony"); //<-- Error at compile process
int total = 0;
foreach (int num in list1 )
{
 total += num;
}

参考: MSDN

ArrayList是不同类型数据的集合,而List<>是其自身依赖的相似类型数据的集合。