difference between boxing and unboxing in c#.net

Difference between boxing and unboxing in C#

C# programming is a strongly-typed language. This is a  most important concept you always get asked in your job interviews that difference between boxing and unboxing in c#.net.

Boxing is storing a value type as an object on the heap, and unboxing is reading the value from the object. You can only unbox the value as it’s exact type.

Casting is when you convert a basic type to another basic type (like from an int to a long), or when you change the type of a reference (like from List<int> to IEnumerable<int>)

Boxing and Unboxing in C# Interview Questions

Boxing and Unboxing is an essential concept in DotNet’s type system. With Boxing and Unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object.

Boxing Unboxing
Definition: Boxing is the process of converting a value types to the reference types. Unboxing is the process of converting a reference type to value types

.

Type of Conversion: Implicit Conversion Explicit Conversion
C# Example: int i = 221;

object obj1 = i; //boxing

object obj2 = 213;

i = (int)obj2 ; // unboxing

 

For more information, see the C# Language Specification.

Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

LEAVE A REPLY

Please enter your comment!
Please enter your name here