T[] 和 List之间有什么区别?

发布于 2024-10-20 18:15:49 字数 528 浏览 5 评论 0原文

实际上我一直在使用通用集合并且经常使用列表<>。 有些场景我认为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慢慢从新开始 2024-10-27 18:15:49

主要区别在于您可以向 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 type T[] 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 using List<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.

一曲爱恨情仇 2024-10-27 18:15:49

要回答您编辑的问题,答案是:这与 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> (or Stack<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.

一笑百媚生 2024-10-27 18:15:49

List 基本上是数组 T[] 的包装器,具有附加方法和 IList 的隐式实现。

另外,typeof(T[]).IsAssignableFrom(typeof(IList)) == true。

在我看来,数组的性能会比列表的性能更好,因为列表所做的一切,它都会对数组做一些事情,而数组只会对自己做一些事情。

The List<T> is basically a wrapper of an array T[] with additional methods and implicit implementation of IList<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.

南冥有猫 2024-10-27 18:15:49

我总是考虑到这一点,在这两者之间进行选择,我也会为您添加一个新的。

  • 数组:我仅将其用于只读访问,因为随机访问是直接的。
  • ArrayList:使用根据需要在内部增长的数组,因此它具有易于随机访问的优点,但如果数组在插入时已满,则必须分配一个新数组并复制所有元素。
  • LinkedList:非常适合插入,如果您想要随机访问,则应避免使用它,因为它必须遍历所有元素,直到出现请求的元素为止。

I always have this in consideration to choose between this two, and i'll add you a new one too.

  • Array: I only use it for read only access because random access is direct.
  • ArrayList: Uses an array internally growing as needed, so it has the advantage of easy random access, but if the array is full on an insertion it has to allocate a new one and copy all the elements.
  • LinkedList: Really good for insertion, should be avoided if you want random access because it has to transverse all the elements until the one requested.
夏の忆 2024-10-27 18:15:49

数组是一种原始语言结构。这意味着,从理论上讲,它们的效率更高。另一方面,它们的功能较少,允许不需要的操作,没有接口,具有有趣属性的多个实现......

除非您有非常严重的性能问题,否则使用集合,而不是数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文