简体   繁体   English

c#中字符串数组和字符串列表有什么区别

[英]what is difference between string array and list of string in c#

I hear on MSDN that an array is faster than a collection.我在MSDN上听说数组比集合快。

Can you tell me how string[] is faster then List<string>.你能告诉我string[]List<string>.有多快吗List<string>.

Arrays are a lower level abstraction than collections such as lists.数组是比集合(如列表)更低级别的抽象。 The CLR knows about arrays directly, so there's slightly less work involved in iterating, accessing etc. CLR 直接了解数组,因此迭代、访问等涉及的工作略少。

However, this should almost never dictate which you actually use.但是,这几乎永远不会决定您实际使用哪个。 The performance difference will be negligible in most real-world applications.在大多数实际应用中,性能差异可以忽略不计。 I rarely find it appropriate to use arrays rather than the various generic collection classes, and indeed some consider arrays somewhat harmful .我很少发现使用数组而不是各种通用集合类是合适的,而且确实有些人认为数组有些有害 One significant downside is that there's no such thing as an immutable array (other than an empty one)... whereas you can expose read-only collections through an API relatively easily.一个显着的缺点是没有不可变数组(空数组除外)之类的东西……而您可以相对轻松地通过 API 公开只读集合。

The article is from 2004, that means it's about .net 1.1 and there was no generics.这篇文章是 2004 年的,这意味着它是关于.net 1.1 的,并且没有泛型。 Array vs collection performance actually was a problem back then because collection types caused a lot of exta boxing-unboxing operations.数组与集合的性能在当时实际上是一个问题,因为集合类型导致了很多额外的装箱-拆箱操作。 But since .net 2.0, where generics was introduced, difference in performance almost gone.但是自从 .net 2.0 引入泛型之后,性能差异几乎消失了。

An array is not resizable.数组不可调整大小。 This means that when it is created one block of memory is allocated, large enough to hold as many elements as you specify.这意味着当它被创建时,会分配一块内存,足够容纳你指定的元素。

A List on the other hand is implicitly resizable.另一方面, List可以隐式调整大小。 Each time you Add an item, the framework may need to allocate more memory to hold the item you just added.每次Add项目时,框架可能需要分配更多内存来保存您刚刚添加的项目。 This is an expensive operation, so we end up saying "List is slower than array".这是一个昂贵的操作,所以我们最终会说“列表比数组慢”。

Of course this is a very simplified explanation, but hopefully enough to paint the picture.当然,这是一个非常简单的解释,但希望足以描绘出这幅画。

An array is the simplest form of collection, so it's faster than other collections.数组是最简单的集合形式,因此它比其他集合更快。 A List (and many other collections) actually uses an array internally to hold its items. List(和许多其他集合)实际上在内部使用一个数组来保存它的项目。

An array is of course also limited by its simplicity.数组当然也受到其简单性的限制。 Most notably you can't change the size of an array.最值得注意的是你不能改变数组的大小。 If you want a dynamic collection you would use a List.如果你想要一个动态集合,你可以使用一个列表。

List<string> is class with a private member that is a string[] . List<string>是一个私有成员是string[] The MSDN documentation states this fact in several places. MSDN 文档在几个地方说明了这一事实。 The List class is basically a wrapper class around an array that gives the array other functionality. List 类基本上是一个围绕数组的包装类,它为数组提供其他功能。

The answer of which is faster all depends on what you are trying to do with the list/array.哪个更快的答案取决于您要对列表/数组做什么。 For accessing and assigning values to elements, the array is probably negligibly faster since the List is an abstraction of the array (as Jon Skeet has said).对于访问元素和为元素赋值,数组的速度可能快到可以忽略不计,因为 List 是数组的抽象(正如 Jon Skeet 所说)。

If you intend on having a data structure that grows over time (gets more and more elements), performance (ave. speed) wise the List will start to shine.如果您打算拥有随时间增长(获得越来越多的元素)、性能(平均速度)的数据结构,那么列表将开始发光。 That is because each time you resize an array to add another element it is an O(n) operation.这是因为每次调整数组大小以添加另一个元素时,都是 O(n) 操作。 When you add an element to a List (and the list is already at capacity) the list will double itself in size.当您向 List 添加一个元素(并且该列表已满)时,该列表的大小将加倍。 I won't get into the nitty gritty details, but basically this means that increasing the size of a List is on average a O(log n) operation.我不会深入细节,但基本上这意味着增加 List 的大小平均是一个 O(log n) 操作。 Of course this has drawbacks too (you could have almost twice the amount of memory allocated as you really need if you only go a couple items past its last capacity).当然,这也有缺点(如果您只使用超过其最后容量的几个项目,则分配的内存量几乎是您真正需要的两倍)。

Edit: I got a little mixed up in the paragraph above.编辑:我在上面的段落中有点混淆。 As Eric has said below, the number of resizes for a List is O(log n), but the actual cost associated with resizing the array is amortized to O(1).正如 Eric 在下面所说的,List 调整大小的次数是 O(log n),但与调整数组大小相关的实际成本分摊到 O(1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM