Introduction
In C#, boxing and unpacking are crucial ideas. Value types (int, char, etc.), Reference types (object), and Pointer types are the three forms of data in the C# type system. In essence, Unboxing does the opposite of Boxing, which transforms a Value Type variable into a Reference Type variable. A unified view of the type system, where a value of any type can be considered as an object, is made possible via boxing and unboxing.
Boxing
Boxing is the process of changing a Value Type (char, int, etc.) variable to a Reference Type (object).
The implicit conversion procedure known as "boxing" takes advantage of object type (supertype).
Reference type variables are kept in Heap memory, whereas Value type variables are always kept in Stack memory.
Example
int rank = 123;
object objRank = rank; //boxing
Explanation
Initially, we create a Value Type variable rank of type int and set its initial value to 123. We now construct a Reference Type variable of type Object called objRank and give it a rank. The Value Type variable rank is implicitly copied and saved in the Reference Type variable objRank as a result of this assignment.
int rank = 123;
object objRank = rank; //boxing
//rank value change
rank = 99;
Console.WriteLine("Value - type value of rank is : {0}", rank);
Console.WriteLine();
Console.WriteLine("Object - type value of objRank is : {0}", objRank);
Unboxing
Unboxing is the process of changing a Reference Type variable into a Value Type variable.
There is a clear conversion procedure.
int rank = 123;
object objRank = rank; // Boxing
int rnk = (int)ObjRank; // Unboxing
Explanation
An integer value of 123 is assigned to the Value Type variable rank, which we declare and declare as having the type int. We now box the number rank in a Reference Type variable of type Object, called objRank. In order to unbox the value from objRank, we now create a Value Type integer rank. Using the casting technique, we specifically state that the object must be cast as an int value in order to accomplish this. As a result, the Reference Type variable from heap memory is copied to stack memory.
int rank = 123;
object objRank = rank; //boxing
//unboxing
int rnk = (int)rank;
Console.WriteLine("Value - type value of rnk is : {0}", rnk);
Console.WriteLine();
Console.WriteLine("Object - type value of objRank is : {0}", objRank);
Important Things to Consider When Boxing and Unboxing C#
- Performance considerations: The processes of boxing and unpacking are computationally demanding. They have an impact on performance, particularly in high-performance settings or tight loops, because they involve memory allocation and copying.
- Type Safety: If the types do not match, unboxing will throw an invalid cast exception at runtime and require an explicit cast.
- Memory Usage: By allocating memory on the heap, boxing may result in higher memory usage and the potential for more frequent garbage collection.
- Use in Collections: Value types were boxed when added to collections such as ArrayList since these collections could only hold objects prior to the introduction of generics in .NET 2.0. boxing can be avoided by using generics (List<T>, Dictionary<TKey, TValue>) since the collections can hold particular value types.
Good Techniques:
- Steer clear of needless boxes and unpacking: Pay attention to the data types you use, particularly when writing performance-critical code, to prevent needless boxing and unboxing.
- Use Generics: Employ generics to prevent value types from being boxed and favor generic collections over non-generic ones.
- Recognizing Implicit Boxing: Recognize when implicit boxing may take place, for as when a value type is passed to a method that takes an object.
We learned the new technique and evolved together.
Happy coding!