Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all C# casts result in boxing/unboxing

Tags:

c#

.net

clr

boxing

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
like image 783
lysergic-acid Avatar asked Feb 20 '12 18:02

lysergic-acid


2 Answers

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.

like image 96
Eric Lippert Avatar answered Sep 20 '22 06:09

Eric Lippert


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)

like image 20
SLaks Avatar answered Sep 20 '22 06:09

SLaks