In this article I am going to discuss about C# boxing and unboxing with examples in detail but before that if you had not read my previous article of C# types of constructors then must read it. C# boxing and unboxing are concepts in csharp which is used to convert the value types to reference types and reference types to values types.
C# Boxing
Boxing is the process of converting the values types like int, float, double to reference types like object. In that case a new object is allocated in heap memory and value is copied to that value. One more important thing is that if an object is created inside heap memory and the reference of that object is stored in stack memory. But if the value types like int, float and double is created then its memory is allocated inside the stack memory. And after the execution complete the memory is automatically released by C# Garbage collector.
C# Unboxing
Unboxing is the process of converting the reference types into value types like int, float and double. In simple words it is the reverse process of boxing. One more thing is unboxing copies the value from the heap to stack memory. Lets understand the C# boxing and unboxing with the help of example.
int firstNumber = 45; //value type -- line 1 object firstObj = x; // Boxing -- line 2 int secondNumber = (int)firstObj //Unboxing -- line 3
In above example you can see how the value type is converted to object. when the line 1 is executed that a stack memory is allocated to our firstNumber. When the line 2 is executed that a object is created inside heap memory and the reference of that object is stored in stack memory. When the line 3 is executed which unbox the object to value type then again the stack memory is allocated to secondNumber but this time the object from heap memory is copied and allocates new memory to stack for secondNumber variable. Let see one program to show the result of above example.
C# Program to explain the Concept.
using System; public class BoxingUnboxingDemo { public static void Main(string[] args) { int firstNumber = 45; Console.WriteLine("Value type : " + firstNumber); object firstObj = firstNumber; Console.WriteLine("Boxing the Value type : "+ firstObj); int secondNumber = (int)firstObj; Console.WriteLine("Unboxing the reference type : "+ secondNumber); } }
Output:
Value type : 45
Boxing the Value type : 45
Unboxing the reference type : 45
Important points related to boxing and unboxing in csharp
- Avoid Unnecessary Boxing and Unboxing : C# Boxing and Unboxing are time consuming operation which can impact on your application performance. Therefore we have to avoid this due to performance degradation in application development.
- Memory Usage : C# Boxing and Unboxing leads to extra memory allocation in heap memory which increase the memory consumption.
- Type Safety : In C# Boxing and Unboxing if you try to use unboxing then you must have to do explicit typecasting which may also leads to InvalidCastException if there is no proper typecasting is there.
- Use Generics : In C# the best alternative for boxing and unboxing is to use the generic type to maintain your application performance.
Conclusion
In this C# tutorials article I have covered the concepts of boxing and unboxing. Hope you understand the concepts and like this article. In my next article I am going to discuss about the Access specifiers in csharp which is one of the very important concepts in programming languages.