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的理论长度是已经定义好的?请多指教。