性能问题:StringCollection 与 List

发布于 2024-12-10 03:58:57 字数 549 浏览 1 评论 0原文

我想知道什么时候应该使用 List< string > 以及何时应该使用 StringCollection

假设我必须处理大量字符串(例如 10mb 的文本文件)。

我知道列表 T> 提供比 字符串集合

但有时我会发现列表< T>例如,当告诉 Gridview 它的数据源是一个 List 时字符串>...

那么有人知道这些集合在内存速度和重量方面的优缺点吗?

关于它们的功能,我相信每个人都会同意说 List 是最好的,所以我的问题不是关于那。考虑问题是关于 Frameworks 4.0 上的项目,因此两者都可以使用。

I was wondering when I should use List< string > and when I should use StringCollection.

Let's say that I have to deal with large number of strings (like text files of 10mb).

I know that List< T > provides more powerful functions than
StringCollection.

But sometimes I kind of find the List< T > slow when for example telling a Gridview that its datasource is a List< String >...

So do anyone know the pros and cons of these collections, concerning speed and weight in memory?

Concerning their functionalities, I am sure everybody will agree to say that List is the best, so my question is not about that. Consider the question is about projects on Frameworks 4.0, so both can be used.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

怎言笑 2024-12-17 03:58:57

我个人更喜欢使用 List

  • 无需记住字符串的一种特定类型
  • 它实现了通用 IEnumerable 而不仅仅是 IEnumerable,因此支持 LINQ
  • 它在 SilverLight 中受支持
  • 对于大多数开发人员来说更惯用(IMO)

我真的很惊讶地发现 StringCollectionList - 看看是否可以用数字来支持它。我犹豫的唯一原因是 GridView 可能对 StringCollection 进行硬编码支持,以使其能够快速使用该类型 - 但这对我来说听起来不太可能。

I would personally prefer to use List<string>:

  • No need to remember one specific type just for strings
  • It implements the generic IEnumerable<T> rather than just IEnumerable, and thus supports LINQ
  • It's supported in SilverLight
  • It's more idiomatic for most developers (IMO)

I would be really surprised to find StringCollection to be significantly faster than List<string> - see if you can back that up with numbers. My only cause for hesitation is that GridView could potentially have hard-coded support for StringCollection to make it fast with that type - but that sounds pretty unlikely to me.

清风挽心 2024-12-17 03:58:57

在性能和效率方面,它们将非常相似。

List 实际上可能会快一点。它是前通用 ArrayList 的包装器。没有装箱/拆箱,但在幕后仍然有一两个额外的步骤,IIRC。

StringCollection 在 .NET 2.0 之前很方便,因为它被强类型化为字符串,这是需要列表的常见事物。不过,我建议现在使用 List 。由于大多数框架和第 3 方程序集将使用它而不是 StringCollection,这将:

  • 避免大量转换,
  • 避免一些混乱。其他(尤其是新的)开发人员会不断地想知道您使用 StringCollection 的原因是什么。

In terms of performance and efficiency, they will be very similar.

List<string> might be a little faster actually. It is kindof a wrapper around the pre-generic ArrayList. There's no boxing/unboxing, but there is still an extra step or two under the hood, IIRC.

StringCollection was handy before .NET 2.0 because it was strongly typed to string, very common thing to want a list of. I would suggest using List<string> now though. Since most framework and 3rd party assemblies will use it rather than StringCollection, this would:

  • avoid a lot of casting
  • avoid some confusion. Other (especially newer) developers would constantly be wondering what your reason was for using StringCollection.
计㈡愣 2024-12-17 03:58:57

List 不是 ArrayList 的包装器,

它是通过数组实现的 ArrayList 的新实现(将其大小调整为双倍)当 count 大于其长度时的大小)和 count 属性。

List<string> is not a wrapper over ArrayList

It is a new implementation of ArrayList implemented by having an array (which is resized to the double size when the count becomes bigger than its length) and a count property.

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