arraylist中的这个属性有什么不同? [英] what is different between this propery in arraylist ?

查看:106
本文介绍了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 ?

推荐答案

解决方案1会如果它解释容量,则是正确的。这并不意味着您无法添加更多项目,而是容量。您可以。但你需要的只是阅读文档:



Solution 1 would be correct if it explained Capacity. It does not mean that you cannot add more items then Capacity. You can. But all you need is to read the documentation:

MSDN 告诉我们:
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<>

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx [ ^ ]。



非泛型类型更糟糕,因为它们需要潜在危险的类型转换。你永远不需要它们。但是,这些类没有标记 [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可以包含的元素数。



Count获取ArrayList中实际包含的元素数。
Capacity Gets or sets the number of elements that the ArrayList can contain.

Count Gets the number of elements actually contained in the ArrayList.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆