ArrayList vs List<>在 C# 中

ArrayList vs Listlt;gt; in C#(ArrayList vs Listlt;gt;在 C# 中)
本文介绍了ArrayList vs List<>在 C# 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#中的ArrayListList<>有什么区别?

What is the difference between ArrayList and List<> in C#?

是否只有 List<> 有类型而 ArrayList 没有?

Is it only that List<> has a type while ArrayList doesn't?

推荐答案

是的,差不多.List<T> 是一个泛型类.它支持存储特定类型的值,而不需要与 object 进行转换(当 TArrayList<中的值类型时,这将产生装箱/拆箱开销/代码>案例).ArrayList 只是存储 object 引用.作为一个泛型集合,List<T> 实现了泛型 IEnumerable 接口,可以在 LINQ 中轻松使用(不需要任何 CastOfType 调用).

Yes, pretty much. List<T> is a generic class. It supports storing values of a specific type without casting to or from object (which would have incurred boxing/unboxing overhead when T is a value type in the ArrayList case). ArrayList simply stores object references. As a generic collection, List<T> implements the generic IEnumerable<T> interface and can be used easily in LINQ (without requiring any Cast or OfType call).

ArrayList 属于 C# 没有泛型的时代.它已被弃用,取而代之的是 List<T>.您不应该在以 .NET >= 2.0 为目标的新代码中使用 ArrayList,除非您必须与使用它的旧 API 交互.

ArrayList belongs to the days that C# didn't have generics. It's deprecated in favor of List<T>. You shouldn't use ArrayList in new code that targets .NET >= 2.0 unless you have to interface with an old API that uses it.

这篇关于ArrayList vs List&lt;&gt;在 C# 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)