Do all C# casts result in boxing/unboxing

后端 未结 3 1672
南方客
南方客 2021-02-07 19:00

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)

相关标签:
3条回答
  • 2021-02-07 19:47

    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."

    0 讨论(0)
  • 2021-02-07 19:47

    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.

    0 讨论(0)
  • 2021-02-07 19:52

    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)

    0 讨论(0)
提交回复
热议问题