Do all C# casts result in boxing/unboxing

[亡魂溺海] 提交于 2019-12-20 18:34:57

问题


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

回答1:


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.




回答2:


No.

Boxing means putting a value into a new reference type instance.

Standard casts between reference types do not result in any allocations.
(User-defined casts can do anything)




回答3:


I am curious to know if all casts in C# result in boxing,

No. Boxing is a very special operation that means treating an instance of a value type as an instance of a reference type. For reference type conversion to reference type conversion, the concept plays no role.

are all casts a costly operation?

Short answer: No.

Long answer: Define costly. Still no, though.

What about casts from 2 different types of reference types? what is the cost of that?

Well, what if it's just a derived to base reference conversion? That's BLAZINGLY fast because nothing happens.

Other, user-defined conversions, could be "slow", they could be "fast."

This one is "slow."

class A { public int Foo { get; set; } }
class B {
    public int Foo { get; set; }
    static Random rg = new Random();
    static explicit operator A(B b) {
        Thread.Sleep(rg.Next());
        return new A { Foo = b.Foo; }
    }
}

This one is "fast."

class A { public int Foo { get; set; } }
class B {
    public int Foo { get; set; }
    static Random rg = new Random();
    static explicit operator A(B b) {
        return new A { Foo = b.Foo; }
    }
}

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

No, it's "cheap."



来源:https://stackoverflow.com/questions/9366251/do-all-c-sharp-casts-result-in-boxing-unboxing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!