What is boxing and unboxing in C#?

In this tutorial, we will learn about the boxing and Unboxing in C#. By IncludeHelp Last updated : April 06, 2023

In C#, there are 2 types: Value type and Reference type. And, the object is the base class of all the types in C#.

When a value type is converted into an object type then this implicit conversion process is called boxing. On the other hand when an object type is converted back to its value type explicitly then it is called unboxing.

Example of boxing and unboxing

Let's understand the concept of boxing and unboxing with the help of below given statements,

int a = 10;

Boxing: In this statement, variable a is boxed and assigned into variable o.

object o = a;

Unboxing: The value of object o need to be unboxed at time of retrieval.

int a =(int)o;

Note: An explicit conversion is required while unboxing process.

boxing and unboxing in C#.Net


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.