【C#食谱】【面食】菜单3:用泛型替代ArrayList
问题:
public static void UseNonGenericArrayList()
{
// Create and populate an ArrayList.
ArrayList numbers = new ArrayList();
numbers.Add(1); // Causes a boxing operation to occur
numbers.Add(2); // Causes a boxing operation to occur
![](/Images/OutliningIndicators/InBlock.gif)
// Display all integers in the ArrayList.
// Causes an unboxing operation to occur on each iteration
foreach (int i in numbers)
{
Console.WriteLine(i);
}
![](/Images/OutliningIndicators/InBlock.gif)
numbers.Clear();
}
public static void UseGenericList()
{
// Create and populate a List.
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
![](/Images/OutliningIndicators/InBlock.gif)
// Display all integers in the ArrayList.
foreach (int i in numbers)
{
Console.WriteLine(i);
}
![](/Images/OutliningIndicators/InBlock.gif)
numbers.Clear();
}
你希望用泛型替代所有的ArrayList以提高应用程序的性能,并使代码更简单。当你发现结构体或其他类型的值保存在这些数据结构中,导致装箱/拆箱操作时,这个是必要的。
解决方法:
把所有出现System.Collections.ArrayList的类用效能更好的泛型System.Collections.Generic.List类替代。
这里有一个用System.Collections.ArrayList类的简单的例子:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这里是一个用System.Collections.Generic.List的简单例子:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
讨论:
由于ArrayList几乎在所有的应用程序中都会用到,因此,这是第一个可以改善性能的好地方。在一些简单的应用程序中,用这种替代方法实现ArrayList是非常简单的。但是,有几点是应该值得注意的。比如,泛型List类并没有实现ICloneable接口,而ArrayList类实现了。
注意,如果没有返回一个同步版本的泛型List,没有返回确定大小的泛型List,那么IsFixedSize和IsSynchronized属性将始终返回false。SyncRoot属性将始终返回一个和它相同的对象。实际上,这个属性返回的是this指针。微软建议,使用lock关键字去锁定整个集合或者是其他你使用的同步的对象。
PS:ArrayList默认的构建大小是16个元素,而List<T>是4个元素。也就是说,如果第17个元素被加到List<T>中,那么,List<T>会重新分配大小3次;而ArrayList只需1次。对于程序的性能,这一点是应该考虑的。
新年就要到了,祝大家新春愉快!
![](http://www.cnblogs.com/CuteSoft_Client/CuteEditor/images/emstar.gif)