arraylist和list< object>中哪个更好?在C#中 [英] Which one is better from arraylist and list<object> in C#

查看:96
本文介绍了arraylist和list< object>中哪个更好?在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我清楚一下这个主题,哪个更适合在arrayList和List< object>之间使用



我尝试过:



毫无疑问列表<>更好,但在这种情况下,即对象列表

Can someone please help me to clear this topic that which one is better to use between arrayList and List<object>

What I have tried:

No doubt List<> is better but what about in this case i.e list of objects

推荐答案

列表< T> 更好,因为它是强类型 - 这意味着它只包含您期望的对象,并且您不必在使用它们时强制它们。



如果您创建列出< object> 然后你扔掉那个强大的打字,你又回到了有效地使用ArrayList,虽然外观更现代。

即这是一个愚蠢的事情 - 总是用完全定义的类型创建一个 List< T>
List<T> is better because it's strongly typed - which means it only contains objects you expect, and you don't have to cast them when you use them.

If you create a List<object> then you throw away that strong typing and you are back to effectively using an ArrayList again, albeit with a more modern appearance.
I.e. it's a stupid thing to do - always create a List<T> with a fully defined type.


对于对象,它在技术上不会问题。



ArrayList(和其他非泛型列表)留在.NET中以保持向后兼容。如果.NET从一开始就支持泛型,它们就不会存在。



所以只要忘记ArrayList存在,所以你的对象列表使用与其他所有类型相同的类型。它使代码更加一致。
For objects it technically does not matter.

ArrayList (and the other non-generic lists) are left in .NET to keep it backwards compatible. They would not have been there if .NET had supported generics from the start.

So just forget ArrayList exist, so your list of objects use the same types as everything else. It keeps the code a bit more consistent.






您的问题的答案有点困难,因为它取决于关于对象的类型,需要对集合进行的收集和操作的长度,甚至那些操作的频率。但是,为了在.Net中获得关于集合性能的良好信息,我将重定向到我的文章,该文章显示了此范围内的基准测试结果。



< a href =https://www.codeproject.com/Articles/1258660/Large-Collections-in-Csharp-Managed-Code-Part-II> C#(托管代码)中的大型集合 - 第二部分 [ ^ ]



仅关于ArrayList vs List我应该提到ArrayList属于.Net没有通用List< t>的时间。实际上,List< t>支持存储特定类型的值而无需转换到对象或从对象转换(无装箱/拆箱开销)。 ArrayList只存储对象引用。



但是,即使你不使用泛型列表的好处,你创建一个具有具体对象类型的列表仍然列表< t> ;由于以下几个原因,它比ArrayList更好:

1- Add方法的低开销。由于检查 Contract.Ensures()

2- 列表< T> <,ArrayList有更昂贵的Add方法/ code>实现 IReadOnlyList< T> ,而 ArrayList 则不实现。当您想构建只读集合时,此intefece非常有用。

3-我建议你看看每个的实现。 ArrayList vs List< t>



如果这不能解决您的问题,请离开评论,我会帮助你改善我的解决方案,直到你的问题得到解决。



干杯,

AH
Hi,

The answer to your question is a bit difficult because it depends on the type of object, length of collection and operations that need to be done on the collection and even those operation`s frequency. But, to get a good landscape about the performance of collections in .Net I will redirect you to my article that shows you results of a benchmarking in this scope.

Large Collections in C# (Managed Code) - Part II[^]

About only ArrayList vs List I should mention that ArrayList belongs to the time that.Net didn`t have generic List<t>. In fact, the List<t> supports storing values of a specific type without casting to or from the object (No boxing/unboxing overhead). ArrayList simply stores object references.

But, even if you don`t use the benefit of generic list and you create a list with a concrete object type still List<t> is better than ArrayList because of several reasons such as:
1- Low overhead in Add method. The ArrayList has more expensive Add method due to checking Contract.Ensures()
2- The List<T> implements the IReadOnlyList<T> while the ArrayList does not. This intefece is very useful when you would like to construct a readonly collection.
3- I would recommand you see the implementation of each of which. ArrayList vs List<t>

If this did not solve your problem then please leave a comment and I will assist you by improving my solution until your problem gets solved.

Cheers,
AH


这篇关于arraylist和list&lt; object&gt;中哪个更好?在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆