类型List中采用泛型T的方式加入内容
而ArrayList中采用object的方式加入内容
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
namespace ListDemo
{
class Program
{
static void Main(string[] args)
{
ValueTypePerfTest();
ReferenceTypePerfTest();
Console.ReadKey();
}
private static void ValueTypePerfTest()
{
const Int32 count = 100000000;
using (new OperationTimer("List<Int32>"))
{
List<Int32> l = new List<int>();
for(Int32 n =0; n < count; n++)
{
l.Add(n);
Int32 x = l[n];
}
l = null;
}
using (new OperationTimer("ArrayList of Int32"))
{
ArrayList a = new ArrayList();
for(Int32 n = 0;

本文探讨了C#中泛型集合List<T>与传统ArrayList<object>的区别。List<T>利用类型参数T确保元素类型安全,而ArrayList<object>则使用object存储,可能需要类型转换。通过实例展示了两者在添加元素时的不同,并分析了其性能和类型安全性方面的差异。
最低0.47元/天 解锁文章
1177

被折叠的 条评论
为什么被折叠?



