何时使用ArrayList在[]数组在C#中? [英] When to use ArrayList over array[] in c#?

查看:125
本文介绍了何时使用ArrayList在[]数组在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用的ArrayList ,而不是一个正常的数组[]。

我觉得如果我作弊(或懒惰)当我使用一个ArrayList,什么时候可以在一个阵列使用ArrayList <?/ p>

解决方案

数组是强类型,并且参数正常工作。如果你知道你收藏的长度,它是固定的,你应该使用数组。

的ArrayList不强类型,每插入或再审将需要一个投地回到你原来的类型。如果你需要采取特定类型的列表的方法,的ArrayList功亏一篑,因为你可以在包含任何类型的ArrayList通过。的ArrayList使用动态扩展阵列内部,所以也有命中,扩大内部数组的大小,当它击中的能力。

您真的想用什么样子列表℃的泛型列表; T&GT; 。这有数组和的ArrayLists的所有优点。它是强类型和它支持的项目的可变长度。

I often use an ArrayList instead of a 'normal' array[].

I feel as if I am cheating (or being lazy) when I use an ArrayList, when is it okay to use an ArrayList over an array?

解决方案

Arrays are strongly typed, and work well as parameters. If you know the length of your collection and it is fixed, you should use an array.

ArrayLists are not strongly typed, every Insertion or Retrial will need a cast to get back to your original type. If you need a method to take a list of a specific type, ArrayLists fall short because you could pass in an ArrayList containing any type. ArrayLists use a dynamically expanding array internally, so there is also a hit to expand the size of the internal array when it hits its capacity.

What you really want to use is a generic list like List<T>. This has all the advantages of Array and ArrayLists. It is strongly typed and it supports a variable length of items.

这篇关于何时使用ArrayList在[]数组在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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