哪个 .net 3.5 集合用于一种类型的对象的可变长度数组?
我需要一个或多或少相当于 C++ std::vector 的 .NET 3.5 类:
- 包含的对象类型是固定的,
- 通过索引随机访问
- 可以创建一个空容器并根据需要添加对象,
我之前使用过 ArrayList
,它是完全正确的,除了它存储 object
并且我必须将检索到的对象转换为正确的类型,并且我可以在其中添加任何内容,这使得编译时类型检查变得更加困难。
是否有类似 ArrayList 的东西,但通过包含的类型进行参数化?
I need a .NET 3.5 class more or less equivalent to C++ std::vector
:
- contained objects type is fixed
- random access by index
- can create an empty container and add objects as needed
earlier I used ArrayList
and it is quite right except it stores object
and I have to cast the retrieved objects to the right type and I can add just anything there and this makes compile-time type checking harder.
Is there anything like ArrayList
but parameterized by contained type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在寻找
List
。例如,要创建整数列表:请注意,您可能希望通过
IEnumerable
、ICollection
或IList< T>
而不是通过具体类型。您不需要 .NET 3.5 来实现这些 - 它们是在 .NET 2 中引入的(此时泛型作为一项功能被引入)。然而,在 .NET 3.5 中,LINQ 使处理任何类型的序列变得更加容易:(
以及更多)。
Sounds like you're after
List<T>
. For example, to create a list of integers:Note that you may wish to expose such lists via
IEnumerable<T>
,ICollection<T>
orIList<T>
rather than via the concrete type.You don't need .NET 3.5 for these - they were introduced in .NET 2 (which is when generics were introduced as a feature). In .NET 3.5, however, there's LINQ which makes working with sequences of any kind easier:
(and much more).
列表
替换
T
与您的类型。例子
List<T>
Substitute
T
with your type.Example