有没有什么情况,不适合用List而应该用ArrayList的?

sangsang28 2013-08-29 03:35:19
ArrayList是最老的容器了吧,C#1时代的,List<T>泛型容器是C#2之后的。

那么请问,有没有什么情形,仍然推荐使用ArrayList而不是List<T>的? 效率问题?
...全文
137 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
djnick 2013-08-30
  • 打赏
  • 举报
回复
List不用装箱拆箱 把上面的例子改成List<int> 试试
threenewbee 2013-08-29
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                var x = new List<object>();
                for (int j = 0; j < 10; j++)
                {
                    x.Add(j);
                }
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);
            sw.Reset();
            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                var y = new ArrayList();
                for (int j = 0; j < 10; j++)
                {
                    y.Add(j);
                }
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);
        }
    }
}
17402617 17170626 Press any key to continue . . .
threenewbee 2013-08-29
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                var x = new List<object>();
                for (int j = 0; j < 100; j++)
                {
                    x.Add(j);
                }
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);
            sw.Reset();
            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                var y = new ArrayList();
                for (int j = 0; j < 100; j++)
                {
                    y.Add(j);
                }
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);
        }
    }
}
测试100个元素的插入,算小数据量了吧。同样在一个数量级上。 94096078 107058149 Press any key to continue . . .
threenewbee 2013-08-29
  • 打赏
  • 举报
回复
为谨慎起见,我实际测试了下。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            List<object>[] a1 = new List<object>[10000000];
            for (int i = 0; i < 10000000; i++)
            {
                a1[i] = new List<object>();
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);
            sw.Reset();
            sw.Start();
            ArrayList[] a2 = new ArrayList[10000000];
            for (int i = 0; i < 10000000; i++)
            {
                a2[i] = new ArrayList();
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);
        }
    }
}
90071119 91062186 Press any key to continue . . . 运行了10多次,互有胜负。
异常异长 2013-08-29
  • 打赏
  • 举报
回复
引用 9 楼 caozhy 的回复:
[quote=引用 6 楼 stubble 的回复:] 当数据量小的时候,ArrayList的操作时间上要比List<T>省。
胡扯。[/quote] List<T>在创建的时候的时间消耗比ArrayList要大?
threenewbee 2013-08-29
  • 打赏
  • 举报
回复
引用 6 楼 stubble 的回复:
当数据量小的时候,ArrayList的操作时间上要比List<T>省。
胡扯。
cheng2005 2013-08-29
  • 打赏
  • 举报
回复
微软官方的观点就是推出List<T>替代ArrayList
异常异长 2013-08-29
  • 打赏
  • 举报
回复
当数据量小的时候,ArrayList的操作时间上要比List<T>省。
  • 打赏
  • 举报
回复
List<object> 和 ArrayList用法上基本无差别 具体性能有无差异,这个没测试过
g4_magicvr 2013-08-29
  • 打赏
  • 举报
回复
需要考虑这点效率的时候 直接抛弃.net库 去玩c或者汇编吧
threenewbee 2013-08-29
  • 打赏
  • 举报
回复
为编写兼容C# 1.0代码的时候。
g4_magicvr 2013-08-29
  • 打赏
  • 举报
回复
脑残的时候……

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧