T[] 和 List之间有什么区别?
实际上我一直在使用通用集合并且经常使用列表<>。 有些场景我认为 new List
非常丑陋,我更喜欢使用 string[]
,但我不使用它,因为就我知道泛型具有更好的性能,因此我使用它们。
string[]、int[] 或任何非通用数组对应用程序有害吗?
我只是想知道数组和通用集合之间的真正区别和影响。
编辑:
让我们假装一个场景
,我必须调用此方法,我应该使用 string[]
还是 List
?什么更好?
static void PrintValues(IEnumerable<string> values) {
foreach(var value in values) {
Console.WriteLine(value);
}
}
Actually I'm always using Generic Collections and I frequently use List<>.
Some scenarios I think that new List<string>()
is very ugly and I prefer to use string[]
, but I don't use it, because as far as I know, Generics has a better performance and therefore I use them.
Is string[], int[] or whatever non-generic-array harmful to the application?
I just wanna know what's really the difference, impact between arrays and generic collections.
edit:
let's fake a scenario
I must call this method, should I use string[]
or List<string>
? What's better?
static void PrintValues(IEnumerable<string> values) {
foreach(var value in values) {
Console.WriteLine(value);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
主要区别在于您可以向
List
添加新元素。在内部
List
将元素存储在T[]
类型的数组中,并且在添加新元素时它会自动分配一个更大的数组(或者在添加新元素时缩小数组)您正在删除元素)。这意味着性能将大致相似。使用List
时存在一些较小的间接性,但 JITter 可能会将其内联。使用
List
的主要原因是它为您提供了更多功能 - 您可以添加和删除元素。编辑在您使用
PrintValues
的示例中,这并不重要。这取决于数据的来源。如果您只想使用一些固定参数来调用它,您可以使用数组,因为它们更容易创建,例如new [] { "hello", "world" }
。在大多数现实场景中,您将从某个地方读取数据 - 如果您可以一次获取所有数据,那么您可以使用数组。如果您要逐一阅读它们,那么您将使用
List
以便您可以在阅读它们时添加元素。The main difference is that you can add new elements to a
List<T>
.Internally
List<T>
stores elements in an array of typeT[]
and it just automatically allocates a larger array when you're adding new elements (or shrinks the array when you're removing elements). This means that the performance will be roughly similar. There is some minor indirection when usingList<T>
, but the JITter may inline that.The main reason for using
List<T>
is that it gives you more functionality - you can add and remove elements.EDIT In your example with
PrintValues
, it doesn't really matter. It depends on the source of the data. If you just want to call it with some fixed arguments you can use arrays, because they're easier to create, e.g.new [] { "hello", "world" }
.In most of the real-world scenarios, you'll be reading the data from somewhere - if you can get all data at once, then you can use arrays. If you'll reading them one-by-one then you'll use
List<string>
so that you can add elements as you read them.要回答您编辑的问题,答案是:这与
PrintValues
函数无关。它只会迭代。重要的是调用这个函数的代码——它是如何得出要打印的值的?如果它们是相当静态的值列表,那么使用数组会更有效。如果需要构建或以其他方式操作集合,则
List
(或Stack
或其他)可能会更好。如果您使用 LINQ,您将更有可能使用
IEnumerable
并且不关心实际实现该接口的类型。To answer your edited question, the answer is: it doesn't matter to the
PrintValues
function. It will simply iterate.What matters is the code that calls this function - how did it come up with the values to print? If they're a fairly static list of values, then using an array would be more efficient. If the collection needs to be built or otherwise manipulated, then a
List<T>
(orStack<T>
or whatever) might be better.If you're using LINQ, you'll be more likely to use
IEnumerable<T>
and to not care what type is actually implementing that interface.List
基本上是数组T[]
的包装器,具有附加方法和IList
的隐式实现。另外,typeof(T[]).IsAssignableFrom(typeof(IList)) == true。
在我看来,数组的性能会比列表的性能更好,因为列表所做的一切,它都会对数组做一些事情,而数组只会对自己做一些事情。
The
List<T>
is basically a wrapper of an arrayT[]
with additional methods and implicit implementation ofIList<T>
.Also,
typeof(T[]).IsAssignableFrom(typeof(IList<T>)) == true
.It seems to me that the performance of an array would be better than the performance of the list, because everything a list does, it does something to an array, and an array just does things to itself.
我总是考虑到这一点,在这两者之间进行选择,我也会为您添加一个新的。
I always have this in consideration to choose between this two, and i'll add you a new one too.
数组是一种原始语言结构。这意味着,从理论上讲,它们的效率更高。另一方面,它们的功能较少,允许不需要的操作,没有接口,具有有趣属性的多个实现......
除非您有非常严重的性能问题,否则使用集合,而不是数组。
An array is a primitive language structure. That means that, theoreticaly, they are more efficient. On the other hand they will have less features, allow unwanted operations, have no interfaces, multiple implementations with interesting properties...
Unless you have very serious performance issues, use Collections, not arrays.