这有什么错一个ArrayList? [英] What's Wrong with an ArrayList?

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

问题描述

最近,我问的SO一个问题,提到可能使用C#ArrayList的一个解决方案。一有人指出,使用一个ArrayList是坏的。我想更多的了解这一点。我从来没有听说过这个说法之前,有关的ArrayList。 可以sombody让我很快地就可能的性能问题,使用的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

c#. .net-2

推荐答案

与ArrayList中的主要问题是使用对象 - 这意味着你必须转换为从无论你是封装。它是仿制前几天的一个遗留下来的,大概是为了向后兼容性。

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 code。

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天全站免登陆