Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Difference between Boxing and Unboxing in C#.Net

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.

Boxing Unboxing
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.
Example

int n = 25; object obj = n;

Example

int k = (int) obj;

Read more Boxing and Unboxing Documentation.

Conclusion

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.

The post Difference between Boxing and Unboxing in C#.Net appeared first on DotNetTec.



This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here

Share the post

Difference between Boxing and Unboxing in C#.Net

×

Subscribe to Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps Api Developer

Get updates delivered right to your inbox!

Thank you for your subscription

×