是将这个ArrayList的一个泛型列表高效? [英] Is converting this ArrayList to a Generic List efficient?
问题描述
在code我写收到来自非托管code一个ArrayList,并且此ArrayList总是包含类型Grid_Heading_Blk的一个或多个对象。我考虑过改变此ArrayList到一个通用的列表,但我不能确定是否转换操作会如此昂贵,以抵消与泛型列表的工作所带来的好处。目前,我只是运行一个的foreach(Grid_Heading_Blk在myArrayList)
操作传递的ArrayList的类,将使用后的ArrayList的内容合作。
The code I'm writing receives an ArrayList from unmanaged code, and this ArrayList will always contain one or more objects of type Grid_Heading_Blk. I've considered changing this ArrayList to a generic List, but I'm unsure if the conversion operation will be so expensive as to nullify the benefits of working with the generic list. Currently, I'm just running a foreach (Grid_Heading_Blk in myArrayList)
operation to work with the ArrayList contents after passing the ArrayList to the class that will use it.
我应该ArrayList的转换为一个通用的类型化列表?如果是的话,什么是这样做的最有效的方法是什么?
Should I convert the ArrayList to a generic typed list? And if so, what is the most efficient way of doing so?
推荐答案
下面是一个刺在高性能的方式来创建一个ArrayList泛型列表。
Here's a stab at a performant way to create a generic list from an ArrayList.
List<Grid_Heading_Blk> myList = new List<Grid_Heading_Blk>(source.Count);
myList.AddRange(source.OfType<Grid_Heading_Blk>());
通过调用的的构造函数,它接受一个int 的,后备存储分配一次。
By calling the constructor that accepts an int, the backing storage is allocated only once.
与往常一样,你应该使用什么工具,你通常使用衡量性能。
As always, you should measure the performance using whatever tools you normally use.
这篇关于是将这个ArrayList的一个泛型列表高效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!