本文介绍了arraylist中的这个属性有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ArrayList alist = new ArrayList();
alist.Add(0);
Console.WriteLine(alist.Count);
Console.WriteLine(alist.Capacity);



这两个属性容量和计数有什么不同?


what is different between this two property Capacity and count ?

推荐答案

告诉我们:
MSDN tells us:

容量总是大于或等于数数。如果在添加元素时Count超过Capacity,则在复制旧元素和添加新元素之前重新分配内部数组会自动增加容量。

Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.





更多重要的是:你永远不应该使用这个列表类型。早在.NET v.2.0中,当引入泛型时,它就被淘汰了。您应该始终使用 System.Collections.Generic (以及专门的集合)中的泛型集合类型。对于列表,请使用 System.Collections.Generic.List<>

[]。



非泛型类型更糟糕,因为它们需要潜在危险的类型转换。你永远不需要它们。但是,这些类没有标记 [Obsolete] 属性,只是因为在遗留代码中使用它们没有任何问题。在新开发中,绝对没有理由使用它们。



-SA



More importantly: you should never use this list type. It is rendered obsolete as early as of the .NET v.2.0, when generics were introduces. You should always use generic collection types from System.Collections.Generic (and also specialized collections). For a list, use System.Collections.Generic.List<>:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^].

Non-generic types are much worse because they require potentially dangerous type cast. You never need them. However, these classes were not marked with [Obsolete] attribute, just because there is nothing wrong with using them in legacy code. In new development, there is absolutely no reason to use them.

—SA



这篇关于arraylist中的这个属性有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:57