是否所有的C#蒙上导致装箱/拆箱 [英] Do all C# casts result in boxing/unboxing

查看:144
本文介绍了是否所有的C#蒙上导致装箱/拆箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道,如果所有的投射在C#中的结果在拳击比赛中,如果没有,都蒙上了代价高昂的操作?

装箱和拆箱(C#编程指南)

  INT I = 123;
    //下面的线框我。
    对象o = I;
 

此行​​显然使拳(结束了int类型为对象)。 这是被认为是昂贵的操作,因为它创建将被收集垃圾。

怎么样从2个不同类型的引用类型强制转换?什么是的费用是多少?能不能正确衡量? (相比于previous例子)

例如:

 大众A级
{
}

公共类B:
{
}

VAR OBJ =新的B();
VAR OBJ2 =(A)目标文件; //这是一个昂贵的操作?这不是拳击
 

解决方案
  

我很想知道,如果所有的转换在C#中的结果在拳击。

没有。只有装箱转换导致拳击,故名装箱转换。拳击转换是从值类型全内置转换为引用类型 - 无论是一类值类型继承,或者它实现了一个接口。 (或一个接口,它实现了一个接口,通过协变或逆变引用转换兼容的。)

  

都转换的代价高昂的操作?

没有。身份转换是零成本,因为编译器的Elid完全它们。

  

什么是隐性和显性引用转换成本?

隐式引用转换的是零成本。编译器可以完全它们的Elid。也就是,从长颈鹿转换为它的基类型的动物,或长颈鹿其实现的接口类型IAmATallMammal,是自由的。

显式引用转换的涉及一个运行时检查,以验证该参考文献并实际上指的是所希望的类型的对象。

这是否运行时检查是昂贵,取决于你的预算。

  

可以在成本合理测算?

当然。决定什么资源是与您 - 时,说一声 - 然后仔细衡量你消耗的时间用秒表

你也没问,但可能是一个问题,应该有:

  

什么是最昂贵的转换?

用户定义的转换只不过是一个语法糖的方法调用;该方法可以采取任意长,像任何方法。

动态转换再次启动编译器在运行时;编译器可能需要任意长的执行类型的分析,这取决于您选择如何努力,分析问题,扔它。

I am curious to know if all casts in C# result in boxing, and if not, are all casts a costly operation?

Example taken from Boxing and Unboxing (C# Programming Guide)

    int i = 123;
    // The following line boxes i.
    object o = i;  

This line obviously causes boxing (wrapping up the int type as an object). This is an operation that is considered costly, since it creates garbage that will be collected.

What about casts from 2 different types of reference types? what is the cost of that? can it be properly measured? (compared to the previous example)

For example:

public class A
{
}

public class B : A
{
}

var obj = new B();
var obj2 = (A)obj; // is this an "expensive" operation? this is not boxing

解决方案

I am curious to know if all conversions in C# result in boxing.

No. Only boxing conversions result in boxing, hence the name "boxing conversions". Boxing conversions are all built-in conversions from value types to reference types -- either to a class that the value type inherits from, or to an interface that it implements. (Or to an interface compatible with an interface it implements, via a covariant or contravariant reference conversion.)

are all conversions a costly operation?

No. Identity conversions are zero cost because the compiler can elide them entirely.

What are the costs of implicit and explicit reference conversions?

Implicit reference conversions are zero cost. The compiler can elide them entirely. That is, converting from Giraffe to its base type Animal, or Giraffe to its implemented interface type IAmATallMammal, are free.

Explicit reference conversions involve a runtime check to verify that the reference does in fact refer to an object of the desired type.

Whether that runtime check is "costly" or not depends on your budget.

can that cost be properly measured?

Sure. Decide what resource is relevant to you -- time, say -- and then carefully measure your consumption of time with a stopwatch.

A question you did not ask but probably should have:

What are the most expensive conversions?

User-defined conversions are nothing more than a syntactic sugar for a method call; that method can take arbitrarily long, like any method.

Dynamic conversions start the compiler again at runtime; the compiler may take arbitrarily long to perform a type analysis, depending on how hard an analysis problem you choose to throw at it.

这篇关于是否所有的C#蒙上导致装箱/拆箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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