Description
Before we talk about boxing and unboxing in C#, let's understand C# types. C# supports two types of types - value type and reference type.
Value Types
A variable representing an object of value type contains the object itself. (It does not contain a pointer to the object). Different value types are:
- Simple Types: Integral Types (sbyte, byte, short, ushort, int, uint, long, ulong), bool type, char type, Floating point types(flaot,double) and the decimal types. They are all aliases of the .NET System Types.
- Struct Types
- Enumeration Types
The value type objects can not be allocated on the managed heap.
Reference Types
A variable representing an object of reference type contains reference or address of the actual data. Reference types are allocated on the managed heap. Different reference types are:
- The Object Type
- The class Type
- Interfaces
- Delegates
- The string type
- Arrays
- Myobj obj1;
- obj1=new myobj();
obj1 is reference type variable (assuming Myobj is a class type variable).compiler allocates memory for this object on the heap and its address is stored in obj1.
Boxing And Unboxing
Having talked about these value types and reference types, now let us talk about boxing and unboxing. Converting a value type to a reference type is called Boxing. Unboxing is an explicit operation. You have to tell which value type you want to extract from the object.
Consider the following code:
- Int32 vt= 10;
- object rt= vt;
- Int32 vt2=(Int32)rt;
Source Code
- using System;
- class BoxAndUnBox
- {
- public static void Main()
- {
- Int32 vt= 10;
- object rt= vt;
- vt=50;
- Int32 vt2=(Int32)rt;
- Console.WriteLine(vt+","+ rt);
- }
- }
Here is another article on
Boxing and Unboxing in C#