C# - Boxing and UnBoxing


Boxing and unboxing are important concepts in C#. Here we willlearn how to use and benifits of Boxing and Unboxing in C#.

1. Boxing in C#

C# allows us to convert a Value Type(char, int etc.)  to a Reference Type(object) process is called Boxing . Value Type variables are always stored in Stack memory.

C# Boxing and Unboxing

C# Boxing and Unboxing

Example

int num = 10;
Object Obj = num; //Boxing

The first line we created a Value Type num and assigned a value to num. The second line , we created an instance of Object Obj and assign the value of num 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.

2. Unboxing in C#

C# allows us to convert a Reference Type(object) to a Value Type(char, int etc.) is called UnBoxing . Reference Type variables are stored in Heap memory.

C# Boxing and Unboxing

Example

int num = 10;
Object Obj = num; //Boxing
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.

Why need boxing and unboxing in C#

To store value types in collections or pass them as object parameters: In C#, collections such as ArrayList and Dictionary require objects as elements, so value types must be boxed to be stored in these collections. Similarly, when passing a value type to a method that takes an object parameter, the value type must be boxed.

Benifits of Boxing and unboxing in C#

  1. Flexibility - Boxing and unboxing allow value types to be treated as objects, which can increase the flexibility and extensibility of a program. This can be especially useful when working with legacy code or third-party APIs that expect objects as parameters or return types.
  2. Compatibility - Boxing and unboxing can help to ensure compatibility between different parts of a program that may use different data types. For example, if a value type must be passed to a method that only accepts objects, boxing can be used to make thevalue type compatible with the method.
  3. Polymorphism - Boxing and unboxing can be useful in cases where polymorphism is needed. For example, if a method accepts an object parameter, it can accept any type of object, including value types that have been boxed.

Drawbacks of Boxing and unboxing in C#

However, it is important to note that there are also some drawbacks to using boxing and unboxing. These include:

  1. Performance - Boxing and unboxing can have performance implications, as they involve creating a new object on the heap or casting an object back to a value type. This can be slow and can lead to memory fragmentation.
  2. Type safety - Boxing and unboxing can also lead to type mismatches or exceptions if the wrong type of object is passed or returned. This can make programs less type-safe and harder to debug.

Prev Next