为什么我应该用泛型替换 CollectionBase?
我不是在寻找如何,而是在寻找为什么?我找不到对此的直接答案。
I'm not looking for how, I'm looking for why? I couldn't find a straight forward answer to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的。 CollectionBase 是之前的尝试,也是提供类型安全的一种方法。
泛型为您提供了这些优势,但还增加了两个巨大的优势:
编辑:
我想到了将代码转移到使用泛型集合的其他几个令人信服的理由:
Cast
(CollectionBase 未实现IEnumerable
,仅实现IEnumerable
)。Yes. CollectionBase was a previous attempt, and a way to provide type safety.
Generics give you these advantages, but add two more HUGE advantages:
Edit:
I thought of a couple of other compelling reasons to move your code to using generic collections:
Cast<T>
(CollectionBase does not implementIEnumerable<T>
, onlyIEnumerable
).只需键入
即可对所有不同类型的集合进行强类型访问?编辑:
如果您要删除现有的和工作的代码,唯一的原因是性能,如其他地方提到的。
Strongly typed access to all the different kinds of collections at the cost of only typing
<type>
?EDIT:
If you are removing existing and working code, the only reason would be for performance as mentioned elsewhere.
类型安全,无需为每种类型编写大量代码。如果您查看 MSDN CollectionBase 文档,您必须编写大量样板代码才能创建类型安全的 Int16 集合。使用泛型,它会稍微短一些:
您不必编写的每一点代码都是不会出错的代码(我这么说是因为它适用于我,我确信它也适用于其他人也一样!
Type safety without having to write a lot of code per-type. If you look at the example in the MSDN CollectionBase documentation, you have to write a lot of boiler-plate code to make a typesafe collection of Int16. With generics it's somewhat shorter:
Every bit of code you don't have to write is code that you won't get wrong (I say that because it applies to me, I'm sure it applies to others too! ;-)
对于 CollectionBase,您需要进行扩展以提供泛型本身具有的功能。您必须编写所有管道以考虑您从其创建的子类中的类型安全性。使用泛型就没有必要了。您需要转换为泛型吗?这取决于您的情况,但我以后不会使用它。
另外,使用泛型而不是集合库可以让您将所有扩展方法写入 .net 中,从而使常见任务变得更容易。 MS 正在推动仿制药的使用,并积极开发仿制药的采用。他们很可能会继续开发其功能。
With CollectionBase you need to extend to provide the functionality that generics do natively. You have to write in all the plumbing to account for type safety in sub classes that you create from it. With generics there is no need to. Do you need to convert to generics? That depends on your situation, but I wouldn't use it going forward.
Plus, using generics instead of collection base gives you all the extension methods written into .net to make common tasks easier. MS is pushing the use of generics and has actively developed their adoption. They will most likely continue to develop their functionality.
您想使用泛型来避免装箱。
这是一篇很好的文章,列出了好处泛型。
You want to use Generics to avoid boxing.
This is a good article listing the Benefits of Generics.
还有另一个原因。
LINQ。
您只能在实现
IEnumerable
的集合上使用 LINQ 方法。要在
CollectionBase
上使用 LINQ,您必须先调用Cast
,或者在类上手动实现IEnumerable
。< br>通过继承
ObjectModel.Collection
,您可以免费获得IEnumerable
实现。There's another reason.
LINQ.
You can only use LINQ methods on a collection that implements
IEnumerable<T>
.To use LINQ on a
CollectionBase
, you must either callCast<T>
first, or manually implementIEnumerable<T>
on the class.By inheriting from
ObjectModel.Collection<T>
, you get anIEnumerable<T>
implementation for free.