LaysomeSmith
LaysomeSmith

Reputation: 681

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?

Upvotes: 9

Views: 1757

Answers (6)

user3446291
user3446291

Reputation:

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

Upvotes: 2

supercat
supercat

Reputation: 81179

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.

Upvotes: 0

Mayank
Mayank

Reputation: 8852

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

Upvotes: 0

Volodymyr Shtenovych
Volodymyr Shtenovych

Reputation: 162

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.

Upvotes: 0

HatSoft
HatSoft

Reputation: 11201

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

Upvotes: 0

Joey
Joey

Reputation: 354576

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.

Upvotes: 8

Related Questions