C# Boxing & Unboxing Tutorial with Examples

C# Boxing & Unboxing Tutorial with Examples

Today, We want to share with you C# Boxing & Unboxing Tutorial with Examples.
In this post we will show you Boxing and Unboxing in C#.NET, hear for C# boxing and unboxing we will give you demo and example for implement.
In this post, we will learn about Understanding Boxing and Unboxing in C# with an example.

Boxing and Unboxing in C#

In this post,We will learn about Boxing and Unboxing in C# with an example.

READ :  VueJS Installation and Configuration - VueJS Environment Setup

Now in this post, I will explain about Boxing and Unboxing in C# with appropriate example. Many time knowingly or unknowingly we use these concepts and it can have disastrous performance issues in application. So in this article we will see what is boxing and unboxing and how does it cause performance problem and how to use them carefully to avoid these issues.

Boxing

So let us start with boxing and below are some points and definitions around it.

  • It is a process of converting value type to the referenced type.
  • Value type variables are stored in Stack Memory

Now create Console Application in Visual Studio and write below lines of code in it.

READ :  VueJs Methods and Event Handling Example

[php]static void Main(string[] args)
{
int count = 10; // Step 1 Declare count of integer datatype.
object obj; // Step 2 :- Declaration of Object obj.
obj = count; // ß Step 3 :- Boxing happens here .Assigning value of count to object obj.
}[/php]

Unboxing

  • Unboxing is a process of converting reference type to the value type.
  • Referenced Type variables are stored in Heap Memory.

[php]
static void Main(string[] args)
{
int count = 10; // Declaration and Initialisation of Variable count of integer datatype.
object obj; // Declaration of Object obj.
obj = count; // Boxing
int j = (int)obj; // Unboxing
}[/php]

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

READ :  C# Code Optimizing Tutorial with Examples

I hope you get an idea about Introduction To C# boxing and unboxing.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment