泛型与数组列表 [英] Generics vs. Array Lists

查看:157
本文介绍了泛型与数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里工作的系统之前,.NET 2.0编写,并没有仿制药的好处。它最终被更新到2.0,但没有code被重构,由于时间的限制。有许多地方,其中code使用该存储的东西作为对象ArraysLists等等。

The system I work on here was written before .net 2.0 and didn't have the benefit of generics. It was eventually updated to 2.0, but none of the code was refactored due to time constraints. There are a number of places where the code uses ArraysLists etc. that store things as objects.

从性能的角度来看,变化多么重要code到使用泛型?我知道从性能比较来看,装箱和拆箱等,这是低效的,但如何提高性能的多少会是否真的有更改吗?是仿制药的东西一展身手前进的基础上使用,或者有足够的性能变化,一个良心应作出努力,以更新旧code的?

From performance perspective, how important change the code to using generics? I know from a perfomance perspective, boxing and unboxing etc., it is inefficient, but how much of a performance gain will there really be from changing it? Are generics something to use on a go forward basis, or it there enough of a performance change that a conscience effort should be made to update old code?

推荐答案

从技术上仿制药的性能,如你所说,更好。但是,除非性能是非常重要的,你已经已在你可能在其他地方所花费的时间来得到更好的改善等各方面进行了优化。

Technically the performance of generics is, as you say, better. However, unless performance is hugely important AND you've already optimised in other areas you're likely to get MUCH better improvements by spending your time elsewhere.

我建议:


  • 使用泛型向前迈进。

  • 如果您有坚实的单元测试,然后重构,以仿制药触碰code

  • 其他花时间做的重构/测量,将显著提高性能(数据库调用,改变数据结构等),而不是在这里和那里几毫秒。

当然,还有比性能等原因更改为仿制药:

Of course there's reasons other than performance to change to generics:


  • 误差不容易,因为你有编译时的类型检查

  • 更易读,你不需要投所有的地方,它的明显的什么类型的存储在一个集合中

  • 如果你使用泛型前进,那么它的清洁到处使用它们

这篇关于泛型与数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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