In this blog post, I will explain one of the most frequently asked c# interview questions about what is the difference between boxing and unboxing in c# with example. In C# we have two kinds of data types, value types, and reference types.
Value type stores the value itself, whereas the reference type stores the address of the value where it is stored. C# introduces 2 methods to Boxing and Unboxing, which links value type to reference type.
What is Boxing and Unboxing in C#
The basic difference between Boxing and Unboxing is that Boxing is the conversion of the value type to an object type whereas, on the other hand, the term Unboxing refers to the conversion of the object type to the value type.
Let’s understand the other differences between Boxing and Unboxing with an example.
|It converts value type to an object type.||It converts an object type into a value type.|
|It’s an implicit conversion process.||Unboxing is the explicit conversion process.|
|The values stored on the stack copied to the object stored on the heap memory.||The object stored on the heap memory copied to the value stored on the stack.|
int n = 25; object obj = n;
int k = (int) obj;
Read more Boxing and Unboxing Documentation.
I hope you liked this article on the difference between boxing and unboxing in c# with example and freshers. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.