我们应该使用通用集合来提高安全性和性能吗? [英] Should we use Generic Collection to improve safety and performance?

查看:57
本文介绍了我们应该使用通用集合来提高安全性和性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们应该使用通用集合来提高安全性和性能吗?

Should we use Generic Collection to improve safety and performance?

推荐答案

当然。你为什么不呢?比性能更重要的是IMO,这是通用API更具表现力的事实。

Of course. Why wouldn't you? More important than the performance, IMO, is the fact that generic APIs are more expressive. That goes for generic APIs in general, not just collections.

编辑:只是澄清一下这些内容:

Just to clarify these a bit:


  • 在通用和非通用集合之间,由于它们的实现方式不同以及整个通用/非通用方面,性能特征略有不同。显然,通用版本避免了装箱/拆箱,并且(在大多数情况下)避免了提取时的执行时间。在实践中,这仅对拳击起作用的价值类型有意义。对于大型集合,内存使用上的差异可能比执行速度差异更重要。

I' m对实际的类型安全性方面的影响较小。这当然是一件好事,但是我不记得曾经因为使用错误的类型(或将其作为错误的类型来获取)而在使用非通用集合时看到了一个错误。

I'm slightly less bothered by the actual type-safety aspect. It's certainly a good thing, but I can't remember ever actually seeing a bug when using the nongeneric collections due to putting the wrong type in (or fetching it as the wrong type). It's certainly a benefit though.

我认为表现力非常重要。我可以看一眼方法的声明,知道返回值的含义-我不需要仔细阅读文档,也不必像笨拙的名称或文档那样给变量。此外,您还可以获得Intellisense等的所有好处。关于语言的问题之一是我可以用它表达什么?和泛型允许表达比以前丰富得多的概念。

I view the expressiveness as very important. I can glance at the declaration of a method and know what to expect from the return value - I don't need to read the documentation quite as closely, or give variables quite as unwieldy names or docs. In addition you get all the benefits of Intellisense etc. One of the questions to ask about a language is "what can I express in it?" and generics allows much richer concepts to be expressed than before.

这篇关于我们应该使用通用集合来提高安全性和性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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