C# 投射一个对象有多昂贵?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1923882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:53:29  来源:igfitidea点击:

How Expensive is Casting an Object?

c#casting

提问by bobber205

Possible Duplicate:
Perfomance of TypeCasting

可能的重复:
TypeCasting 的性能

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.

想看看别人怎么看这个。我敢肯定这是非常特殊的情况。

采纳答案by LBushkin

You should avoid worrying about the performance implications of specific language features unless you have specific evidence (measurements) that they are actually causing a problem.

您应该避免担心特定语言功能的性能影响,除非您有具体的证据(测量)表明它们实际上导致了问题。

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

您主要关注的应该是代码的正确性及其可维护性。

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.

然而,作为一般观察,在 C# 中通常可以通过应用良好的 OO 编程实践和适当地使用泛型(特别是集合)来避免不必要的强制转换。在您确实需要执行强制转换的情况下,它不太可能成为性能瓶颈,除非您在紧密循环中执行此操作或使用可能引发无效强制转换异常的类型。

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

大多数现实世界的性能问题来自算法选择或缺乏对平台本身的认识 - 而不是来自特定的语言功能。

回答by Tamas Czinege

No, it shouldn't be avoided at all costs. Casting isn't very expensive. Of course, if you have a loop that runs a million times a second it might make sense to avoid casting to save some performance, otherwise it won't really cause performance issues.

不,不应该不惜一切代价避免它。铸造不是很贵。当然,如果你有一个每秒运行一百万次的循环,避免强制转换以节省一些性能可能是有意义的,否则它不会真正导致性能问题。

The real problem with casting is that it's cheating type safety. If you're not careful, it's not too hard to introduce bugs or decrease the readability of the code if you cast things all over the place.

铸造的真正问题在于它的作弊类型安全。如果你不小心,如果你到处乱扔东西,引入错误或降低代码的可读性并不太难。

回答by Lukasz

If you can use generics then it is a better solution than casting. Boxing and unboxing is an expensive operation that should be avoided if possible. The problem is sometimes it just can't be avoided.

如果您可以使用泛型,那么它是比强制转换更好的解决方案。装箱和拆箱是一项代价高昂的操作,应尽可能避免。问题是有时无法避免。

Also the other answer here mentioned that worrying about something as boxing is very trivial compared to specific performance concerns or code maintainability. I completely agree with that.

这里的另一个答案还提到,与特定的性能问题或代码可维护性相比,担心装箱是非常微不足道的。我完全同意这一点。

回答by AdaTheDev

Generally, the cost of casting an object on an adhoc basis is low in the grand scale of things. However, if you are repeatedly casting objects many times then that is when you should try to avoid it if you find it is a cause of a performance issue.

通常,在事物的大尺度上,临时铸造对象的成本较低。但是,如果您多次重复投射对象,那么如果您发现它是性能问题的原因,那么您应该尝试避免它。

After all, one of the main improvements from .NET 1.1 to 2.0 was the introduction of generics - this addressed the issue of strongly typed lists of objects (e.g. ArrayList = based on object, List = typed list)

毕竟,从 .NET 1.1 到 2.0 的主要改进之一是引入了泛型——这解决了强类型对象列表的问题(例如 ArrayList = 基于对象,List = 类型列表)