现在的位置: 首页 > 综合 > 正文

c# arraylist 理论最大长度?

2013年10月06日 ⁄ 综合 ⁄ 共 564字 ⁄ 字号 评论关闭

c# arraylist 理论最大长度?

设计思路如下:首先选择一个超大的数字,作为循环的终止数,在循环中不断地向数组中添加元素,当出现内存溢出的异常OutOfMemoryException 时,输出数组的count属性,此时的count就是arraylist的最大长度。

代码如下:

    public static void Main()
    {
        ArrayList arr = new ArrayList();
        try
        {
            for (int i = 0; i < 10000000000; i++)
            {
                arr.Add(1);
            }
            Console.WriteLine("ok");
            Console.WriteLine(arr.Count);
        }
        catch (OutOfMemoryException ex)
        {
            Console.WriteLine("error");
            Console.WriteLine(arr.Count);
            //throw;
        }
    }

输出结果:

error
67108864

是否意味着arraylist的最大长度就是67108864?

不知这么做是否多此一举?是否 arraylist的理论长度是已经定义好的?请多指教。

抱歉!评论已关闭.