使用类型安全集合类有什么好处?
我想知道,为什么在某些情况下我会看到一个代表某种类型集合的类。
例如:
在Microsoft XNA框架中:TextureCollection、TouchCollection等。 还有 .NET 框架本身中的其他类,以 Collection 结尾。
为什么要这样设计呢?以这种方式而不是像 C# 2.0 中引入的泛型类型集合有什么好处?
谢谢
I was wondering, why on some occasions i see a class representing some type's collection.
For example:
In Microsoft XNA Framework: TextureCollection, TouchCollection, etc.
Also other classes in the .NET framework itself, ending with Collection.
Why is it designed this way? what are the benefits for doing it this way and not as a generic type collection, like was introduced in C# 2.0 ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你举的例子都很好。 TextureCollection 是密封的,没有公共构造函数,只有一个内部构造函数。 TouchCollection 实现
IList
,类似于List
实现IList
的方式。顺便说一句,泛型在这里工作,赞成的答案是不正确的。TextureCollection 被故意削弱,它确保您永远无法创建它的实例。只有关于纹理的秘密知识才能填充这个集合,一个List<>还不够,因为它无法使用使索引器工作的秘密知识进行初始化。该类也不需要是通用的,它只知道纹理类实例。
TouchCollection 也同样专业化。 Add() 方法抛出 NotSupportedException。这不能用常规的 List<> 来完成。类,它的 Add() 方法不是虚拟的,因此不能重写以引发异常。
这并不罕见。
The examples you gave are good ones. TextureCollection is sealed and has no public constructor, only an internal one. TouchCollection implements
IList<TouchLocation>
, similar to the wayList<T>
implementsIList<T>
. Generics at work here btw, the upvoted answer isn't correct.TextureCollection is intentionally crippled, it makes sure that you can never create an instance of it. Only secret knowledge about textures can fill this collection, a List<> wouldn't suffice since it cannot be initialized with that secret knowledge that makes the indexer work. Nor does the class need to be generic, it only knows about Texture class instances.
The TouchCollection is similarly specialized. The Add() method throws a NotSupportedException. This cannot be done with a regular List<> class, its Add() method isn't virtual so cannot be overridden to throw the exception.
This is not unusual.
在 .NET 框架本身中,许多类型安全集合早于 2.0 泛型,并且保留是为了兼容性。
对于一些与 XAML 相关的上下文,要么没有语法来指定泛型类,要么语法很麻烦。因此,当使用
List
时,会针对每种需要编写特定的TList
。In the .NET framework itself, many type-safe collections predate 2.0 Generics, and are kept for compatibility.
For several XAML-related contexts, there's either no syntax to specify a generic class, or the syntax is cumbersome. Therefore, when
List<T>
wiould be used, there's a specificTList
written for each need.它允许您在集合上定义自己的语义(您可能不希望有
Add
或AddRange
方法等...)。此外,代码的可读性不会随处可见
List
和List
。还有相当多的 .NET 1.0/1.1 代码仍然需要工作,因此早于泛型的旧集合仍然需要存在。
It allows you to define your own semantics on the collection (you may not want to have an
Add
orAddRange
method etc...).Additionally, readability is increased by not having your code littered with
List<Touch>
andList<Texture>
everywhere.There is also quite a lot of .NET 1.0/1.1 code that still needs to work, so the older collections that predate generics still need to exist.
例如,在 XAML 中使用泛型类并不那么容易。
It's not that easy to use generic classes in XAML for example.
根据 Oded 的回答,当您决定需要堆栈/队列等而不是
List
时,您自己的类类型可以更轻松地进行更改。造成这种情况的原因可能有很多,包括性能、内存使用等。事实上,隐藏此类实现细节通常是个好主意 - 类的用户只是想知道它存储
纹理
,而不是如何。Following on from Oded's answer, your own class type allows for much easier change down the track when you decide you want a stack / queue etc instead of that
List
. There can be lots of reasons for this, including performance, memory use etc.In fact, it's usually a good idea to hide that type of implementation detail - users of your class just want to know that it stores
Textures
, not how.