简体   繁体   中英

Boxing and unboxing is also casting?

When we convert the data types between primitive data types it is called as data type casting.

But when convert between ValueType and ReferenceType we call it as boxing and unboxing.

Can boxing and unboxing also be called casting?

Boxing is just wrapping a value type in an object hull, essentially. It doesn't actually involve a type conversion as, say, (int)3.14 does. Even though they both use the cast operator.

C# Type System contains three Types , they are Value Types , Reference Types and Pointer Types. C# allows us to convert a Value Type to a Reference Type, and back again to Value Types . The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.

Boxing

  1. int Val = 1;
  2. Object Obj = Val; //Boxing

The first line we created a Value Type Val and assigned a value to Val. The second line , we created an instance of Object Obj and assign the value of Val to Obj. From the above operation (Object Obj = i ) we saw converting a value of a Value Type into a value of a corresponding Reference Type . These types of operation is called Boxing.

UnBoxing

  1. int Val = 1;
  2. Object Obj = Val; //Boxing
  3. int i = (int)Obj; //Unboxing

The first two line shows how to Box a Value Type . The next line (int i = (int) Obj) shows extracts the Value Type from the Object . That is converting a value of a Reference Type into a value of a Value Type. This operation is called UnBoxing.

Boxing and UnBoxing are computationally expensive processes. When a value type is boxed, an entirely new object must be allocated and constructed , also the cast required for UnBoxing is also expensive computationally

Boxing is the process to converting a value type to the type object and unboxing retriveing the value back http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx

Well as casting is to convert one type to another compatible type

Boxing is basically boxing a value type into a anonymous object which later can be unboxed.

int x = 567;
object y = x; //boxing

int z = (int) y; //z will be 123

int x = 567;
object y = (object)x; //explicit boxing (not necessary)

int z = (int) y; //z will be 123

Boxing/Unboxing should not be confused with type casting as while boxing we are just putting a wrapper around a variable. By doing the type casting you are actually changing the type of the variable or object.

Double x = 3.14444;
Integer y = (Integer)x; //(type cast or static cast in c++) changing the type and loosing the original value too in this case.


int x = 567;
object y = (object)x; //explicit boxing (not necessary)

float z = (float) y; //another example of type casting while unboxing

Casting: (it's basically about converting instance of one type into another)

int a = (int) 3.14 // from the example above does casting with loosing precision.
double b = a; // casting again (we may write (double) a - gives the same result).

Boxing: (process of copying value type to the heap)

object c = new MyStruct(); // MyStruct = struct.
object d = 1;
object e = (int) 3.14; // here we have actually casting and then boxing.

Unboxing (copying boxed value type back to the stack):

Button f = (MyStruct) c;
int g = (int) d; // it still unboxing however it looks exactly like the line #1 above.

Hope it helps.

Every non-nullable value type has an associated heap-object type with identical members. Boxing a value type storage location creates a new heap-type instance and copies all the fields from the value-type storage location to the corresponding fields of the new instance. Unboxing a value type copies all the fields from an instance of its corresponding heap-object type to the corresponding fields of a value-type storage location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM