使用 String[] 相对于 List有什么好处?
Possible Duplicates:
c# array vs generic list
Array versus List<T>: When to use which?
I understand that there are several benefits of using List<>. However, I was wondering what benefits might still exist for using arrays.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将有一个简单的静态结构来保存项目,而不是与列表相关的开销(动态调整大小、插入逻辑等)。
但在大多数情况下,列表的灵活性和适应性比这些好处更重要。
You'll have a simple static structure to hold items rather than the overhead associated with a List (dynamic sizing, insertion logic, etc.).
In most cases though, those benefits are outweighed by the flexibility and adaptability of a List.
数组相对于列表的一件事是协方差。
One thing arrays have over lists is covariance.
数组比列表更简单,因此开销更少。如果您只需要数组的功能,则没有理由使用列表。
数组是最简单的集合形式,大多数其他集合都以某种形式使用。列表实际上在内部使用数组来保存其项目。
每当语言构造需要轻量级的一次性集合时,它就会使用数组。例如这段代码:
实际上在幕后变成了这段代码:
An array is simpler than a List, so there is less overhead. If you only need the capabilities of an array, there is no reason to use a List instead.
The array is the simplest form of collection, that most other collections use in some form. A List actually uses an array internally to hold it's items.
Whenever a language construct needs a light weight throw-away collection, it uses an array. For example this code:
actually becomes this code behind the scene:
如果您的集合是不可变的,我只会使用数组。
编辑:不可变在某种意义上你的收藏不会增长或缩小。
I would only use an array if your collection is immutable.
Edit: Immutable in a sense that your collection will not grow or shrink.
速度将是主要的好处,除非您开始编写自己的代码来插入/删除项目等。
列表在内部使用数组,并且只管理列表在内部为您做的所有事情。
Speed would be the main benefit, unless you start writing your own code to insert/remove items etc.
A List uses an array internally and just manages all of the things a list does for you internally.