ArrayList 有什么问题? [英] What's Wrong with an ArrayList?

查看:31
本文介绍了ArrayList 有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我问了一个关于 SO 的问题,其中提到了可能使用 c# ArrayList 作为解决方案.有人评论说使用数组列表是不好的.我想更多地了解这一点.我以前从未听说过关于数组列表的这种说法.有人可以让我了解使用数组列表可能出现的性能问题吗

Recently I asked a question on SO that had mentioned the possible use of an c# ArrayList for a solution. A comment was made that using an arraylist is bad. I would like to more about this. I have never heard this statement before about arraylists. could sombody bring me up to speed on the possible performance problems with using arraylists

c#..net-2

推荐答案

ArrayList 的主要问题在于它使用了 object - 这意味着您必须在封装的任何内容之间进行转换.这是泛型之前的残余,可能只是为了向后兼容.

The main problem with ArrayList is that is uses object - it means you have to cast to and from whatever you are encapsulating. It is a remnant of the days before generics and is probably around for backwards compatibility only.

ArrayList 没有泛型列表的类型安全.性能问题是需要将对象转换回原始对象(或发生隐式装箱).

You do not have the type safety with ArrayList that you have with a generic list. The performance issue is in the need to cast objects back to the original (or have implicit boxing happen).

每当您使用值类型时,都会发生隐式装箱 - 将其放入 ArrayList 时会被装箱,而在被引用时会被取消装箱.

Implicit boxing will happen whenever you use a value type - it will be boxed when put into the ArrayList and unboxed when referenced.

问题不仅仅是性能问题,还有可读性和正确性的问题.自从泛型出现以来,这个对象已经过时了,只有在 .NET 1.0/1.1 代码中才会需要.

The issue is not just that of performance, but also of readablity and correctness. Since generics came in, this object has become obsolete and would only be needed in .NET 1.0/1.1 code.

这篇关于ArrayList 有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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