Generics vs. Array Lists

后端 未结 12 1537
孤独总比滥情好
孤独总比滥情好 2020-12-10 12:19

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

12条回答
  •  佛祖请我去吃肉
    2020-12-10 12:49

    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.

    I would suggest:

    • use generics going forward.
    • if you have solid unit tests then refactor to generics as you touch code
    • spend other time doing refactorings/measurement that will significantly improve performance (database calls, changing data structures, etc) rather than a few milliseconds here and there.

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

    • less error prone, since you have compile-time checking of types
    • more readable, you don't need to cast all over the place and it's obvious what type is stored in a collection
    • if you're using generics going forward, then it's cleaner to use them everywhere

提交回复
热议问题