c# 什么时候应该使用List,什么时候应该使用arraylist?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/725459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:39:24  来源:igfitidea点击:

c# When should I use List and when should I use arraylist?

c#listarraylist

提问by Scott

As the title says when should I use Listand when should I use ArrayList?

正如标题所说,我List应该什么时候使用,ArrayList什么时候应该使用?

Thanks

谢谢

采纳答案by Marc Gravell

The main time to use ArrayListis in .NET 1.1

主要使用时间ArrayList是在.NET 1.1

Other than that, List<T>all the way (for your local T)...

除此之外,List<T>一路(对于您当地的T)......

For those (rare) cases where you don't know the type up-front (and can't use generics), even List<object>is more helpful than ArrayList(IMO).

对于那些您不知道类型预先(并且不能使用泛型)的(罕见)情况,甚至List<object>ArrayList(IMO)更有帮助。

回答by Frederik Gheysels

Since List is a generic class, I would tend to always use List.

由于 List 是一个泛型类,我倾向于总是使用 List。

ArrayList is a .NET 1.x class (still available & valid though), but it is not 'typed'/generic, so you'll need to cast items from 'object' back to the desired type; whereas when using List, you don't have to do that.

ArrayList 是一个 .NET 1.x 类(尽管仍然可用且有效),但它不是“类型化”/通用的,因此您需要将项目从“对象”转换回所需的类型;而在使用 List 时,您不必这样做。

回答by JohnIdol

You should always use List<TypeOfChoice>(introduced in .NET 2.0 with generics) since it is TypeSafe and faster than ArrayList(no un-necessary boxing/unboxing).

您应该始终使用List<TypeOfChoice>(在带有泛型的 .NET 2.0 中引入),因为它是 TypeSafe 并且比ArrayList(没有不必要的装箱/拆箱)更快。

Only case I could think of where an ArrayList could be handy is if you need to interface with old stuff (.NET 1.1) or you need an array of objects of different type and you load up everything as object - but you could do the latter with List<Object>which is generally better.

我能想到的唯一一种 ArrayList 可以方便的情况是,如果您需要与旧东西(.NET 1.1)进行交互,或者您需要一个不同类型的对象数组,并且您将所有内容都作为对象加载 - 但您可以做后者用List<Object>哪个更好。

回答by Niran

Use List where ever possible. I can't see any use to ArrayList when high performing List exists.

尽可能使用 List。当存在高性能 List 时,我看不到 ArrayList 有任何用处。

回答by Timotei

As other said. You should use the List generic, almost always when you know the type (C# is a strong-typed language), and other ways when u do polymorphic/inhertance classes or other stuff like that.

正如其他人所说。您应该使用 List 泛型,几乎总是在您知道类型(C# 是一种强类型语言)时,以及在您执行多态/继承类或其他类似内容时的其他方式。

回答by blitzkriegz

ArrayList is an older .NET data structure. If you are using .NET 2.0 or above always use List when the array needs to hold items of the same type. Usage of List over ArrayList improves both performance and usability.

ArrayList 是一种较旧的 .NET 数据结构。如果您使用 .NET 2.0 或更高版本,当数组需要保存相同类型的项目时,请始终使用 List。在 ArrayList 上使用 List 可以提高性能和可用性。

回答by VIRA

If you dont want to use Linq Queries, then you dont need to use List. If you want to use then you must prefer List.

如果你不想使用 Linq 查询,那么你就不需要使用 List。如果你想使用,那么你必须更喜欢 List。

回答by kuttychutty

Generics was introduced in .Net 2.0.If you are using earlier versions of .Net ,then you can use the Array List else we can go with the Generic List itself. Array List is the deprecated one and won't provide better type safety and also create boxing and unboxing problems.But Generic List won't.

泛型是在 .Net 2.0 中引入的。如果您使用的是 .Net 的早期版本,那么您可以使用数组列表,否则我们可以使用泛型列表本身。Array List 已被弃用,不会提供更好的类型安全性,也会产生装箱和拆箱问题。但 Generic List 不会。