我应该使用 ArrayList 还是 IList
我使用 .NET Framework 1.1,希望有人可以帮助我实现动态对象数组?
下面是我希望使用的对象的简化示例。
Class CarObj
{
public string CarName;
public string CarYear;
}
我应该使用 ArrayList 还是您认为创建 CarList 类来实现 IList 接口会更好?两者之间是否存在性能优势?
Im using the .NET framework 1.1 and Im hoping someone could help me implement a dynamic array of objects?
A watered-down example of the object I wish use is below.
Class CarObj
{
public string CarName;
public string CarYear;
}
Should I use an ArrayList or do you think it would be better to make a CarList class to implement an IList interface? Is there a performance benefit for one over another?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
继承
CollectionBase
相反;它包装了IList
并允许您提供自己所需的类型化方法的实现。这就是它的设计目的:)Inherit
CollectionBase
instead; this wrapsIList
and allows you to provide your own implementation of typed methods you need. This is what it's designed for :)如果您使用 1.1 并且需要动态数组样式集合,那么 ArrayList 是让您行动的最佳选择。
不过,我确实讨厌使用 ArrayList 带来的所有转换,而且我当然不会责怪人们编写在 1.1 中强烈实现了成员的自定义集合。但是,如果您确实走这条路,我建议您使用自定义生成器而不是繁琐的手动编码。有几个可用的可以通过谷歌快速找到,
例如这个链接实际上会在网站上为您生成代码,并且可以简单地将其复制并粘贴到您的项目中。
If you are using 1.1 and need a dynamic array style collection then
ArrayList
is the best choice to get you moving.I do hate all of the casting that comes with using
ArrayList
though and I certainly don't fault people for writing custom collections that have strongly implemented members in 1.1. However if you do go this route I would suggest using a custom generator vs. tedious hand coding. There are several available that can quickly be found via googleThis link for example will actually generate the code for you on the website and it can be simply copy and pasted into your project.
实现您自己的集合类(因为您陷入了 1.1)的主要好处是类型安全;当您确定您的集合只能包含
CarObj
实例时,无需进行类型转换或担心转换错误。在内部,该集合类可以使用 ArrayList 来存储对象。The major benefit of implementing your own collection class (since you are stuck in 1.1) is type safety; when you know for sure that your collection can contain only
CarObj
instances, there is no need for type casting or worrying about conversion errors. Internally this collection class could use anArrayList
to store the objects.