多么昂贵的铸造对象? [英] How Expensive is Casting an Object?

查看:119
本文介绍了多么昂贵的铸造对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
类型转换的性能比较

如何昂贵的是它为对象转换为另一个对象?

How expensive is it to cast as object as a another object?

CustomClass instance = GenericObject as CustomClass

它应该是可以避免的,因为所有的费用?

Should it be avoided as all costs?

想看看别人怎么想这个问题。我敢肯定,这是非常情境。

Wanting to see how others think about this. I'm sure it's very situational.

推荐答案

你应该避免担心的特定语言特征,除非你对性能的影响有具体的证据(测量),他们实际上是导致问题。

您首要关注应该是代码的正确性和它的可维护性。

Your primary concerns should be the correctness of the code and it's maintainability.

作为一般的观察,但是,不必要的铸造经常可以在C#只是通过应用良好的面向对象的编程习惯和使用泛型(特别是集合)避免恰如其分。当你需要进行铸造的情况下,这是极不可能成为性能瓶颈,除非你在一个紧密的循环或有可能抛出一个无效的转换异常类型这样做。

As a general observation, however, unnecessary casting can often be avoided in C# just by applying good OO programming practices and using generics (particularly the collections) appropriately. In those cases where you do need to perform casting, it's highly unlikely to be a performance bottleneck unless you're doing it in a tight loop or with types that are likely to throw an invalid cast exception.

大多数现实世界中的性能问题,从算法选择,或者缺乏对平台本身的认识出现 - 而不是从具体的语言特征

Most real world performance problems emerge from algorithm choices or a lack of awareness of the platform itself - not from specific language features.

这篇关于多么昂贵的铸造对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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