What is the difference between boxing / unpacking and type of casting?

What is the difference between boxing / unpacking and type of casting?

Often the terms appear to be used interchangeably.

+58
casting boxing unboxing
Jul 06 '09 at 2:17
source share
5 answers

Boxing refers to converting a type with a null value to a reference type or converting a value type to some interface that it implements (say int - IComparable<int> ). In addition, converting a base value type to a type with a zero value is also a box conversion. (Caution: Most discussions on this issue ignore the last two types of transformations.)

For example,

 int i = 5; object o = i; 

converts i to an instance of type object .

Unboxing refers to explicit conversion from an object or ValueType instance to a type with an invalid value, conversion of an interface type to a type with an invalid value (for example, IComparable<int> to int ). In addition, converting from NULL to a base type is also a conversion for unpacking. (Caution. Most discussions of this subject will ignore the last two types of transformations.)

For example,

 object o = (int)5; int i = (int)o; 

converts an integer placed in o to an instance of type int .

Type listing is an explicit conversion of an expression to a given type. In this way,

 (type) expression 

explicitly converts expression to an object of type type .

+41
Jul 06 '09 at 2:29
source share

Boxing and unboxing are a subset of types. Boxing is the act of processing a value type as a reference type (which in practice involves copying the contents of this value type (from the stack) to the heap and returning a link to this object). This allows you to pass a value type wherever a compatible reference type is expected. It also allows you to make virtual method calls and other functions of reference types by value type. Unboxing is the reverse operation (returning the type of a value from an object in a box).

The type cast is a term used for any type of conversion from a variable of a particular type to another. This is a broader concept.

A few minutes ago I answered a corresponding question that covers this difference . To summarize, I have classified the different types of IL instructions generated by the C # cast operator:

  • Box ( box IL-instruction) and unboxing ( unbox IL-command)
  • Passing through inhertiance hierarchy (e.g. dynamic_cast<Type> in C ++ uses castclass IL check)
  • Listing between primitive types (e.g. static_cast<Type> in C ++, there are many IL statements for different types of drops between primitive types)
  • Call custom conversion operators (at the IL level, they are just method calls for the corresponding op_XXX method).
+19
Jul 6 '09 at 2:19
source share

Boxing is a term for converting a value type (int, double, float, Guid, etc.) into a reference type (System.Object, System.String, etc.). Performing this boxing operation allocates memory to the heap (which the garbage collector will eventually need to return). Unboxing is the inverse of this process, using a reference type and turning it into a value type.

Casting takes a type (say, System.Object) and treats it as another type (say, System.String).

When you put something in C #, you translate it to another type. The difference is that it allocates additional memory when creating a new reference type .

Bottom line: boxing is a special type of cast that converts a value type to a reference type, which requires the allocation of a new reference type.

+15
Jul 6 '09 at 2:23
source share

Boxing / unpacking and type casting are two different operations, but they use the same syntax.

They are used only interchangeably when the person talking about this does not know what is really happening ...

Boxing saves the value type as an object on the heap, while unboxing reads the value from the object. You can only delete the value as an exact type.

Casting is the conversion of a base type to another base type (for example, from int to long ) or when changing a link type (for example, from List<int> to IEnumerable<int> ).

+4
Jul 06 '09 at 2:23
source share

Boxing means converting a variable of a value type (i.e., an integer) to a reference type. Unboxing is the other way around using type casting. In the .NET world, everything comes from the "object" type in a nutshell.

For example (C # example):

 int myInt = 0; // original variable (unboxed to begin with) object boxed = myInt; // box it up int myIntUnBoxed = (int)boxed; // and unbox it again using type casting 

The conclusion from this is the unification of the type system, allowing us to consider value types as reference types. This article takes a deeper look at boxing / unboxing.

+3
Jul 06 '09 at 2:36
source share



All Articles