Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies.
I love to learn new things in life that keep me motivated.
In C#, boxing and unboxing are two related concepts related to treating values and expressions. Understanding these concepts is important for effective memory management and optimal performance in .NET applications.
Boxing
Boxing is the process of converting a value type (such as int, float, struct, etc.) to a reference type (specifically, an object). Create an object on this heap and copy the value to this other object.
Syntax-
int num = 123; // Value type
object obj = num; // Boxing
Here is, the integer num is boxed into an object obj.
Unboxing
Unboxing is the opposite of boxing. This involves converting the boxed product back to its corresponding value. This requires an explicit cast and of course copying the value back to a variable value type.
Syntax-
object obj = 123; // Boxing
int num = (int)obj; // Unboxing
Here is, The object obj unboxed into an integer num
Example-
using System;
namespace myProgram{
class Program
{
public static void Main()
{
// Boxing value
int num = 42;
object obj = num; // num is boxed into obj
Console.WriteLine("Boxed value: " + obj);
// Unboxing value
int unboxedNum = (int)obj; // obj is unboxed back to an int
Console.WriteLine("Unboxed value: " + unboxedNum);
}
}
}
Output-
Boxed value: 42
Unboxed value: 42
Key Points
Boxing
Conversion implicitly
This includes creating objects on the heap.
There may be performance overhead due to memory allocation and garbage collection.
Unboxing
Conversion explicitly
It may throw an InvalidCastException if the object does not have a valid target type.
Performance Considerations
Boxing unboxing can have important business implications because,
Boxing heaps allocates memory, which is costly in terms of performance compared to stack allocation.
Frequent boxing and unboxing can increase garbage collection, affecting application performance.
Avoiding Boxing and Unboxing
Consider the following methods to avoid unnecessary boxing and unboxing,
Use regular collections of objects (such as List<T>,
Dictionary<TKey, TValue>) instead of irregular objects (such as
ArrayList, Hashtable, etc.) to avoid value type boxing.
Reduce the use of object type variables for storing values unless absolutely necessary.
By carefully understanding and managing boxing and unboxing, you can write more efficient and better-performing C# code.
Ashutosh Kumar Verma
11-Jun-2024C# Boxing and Unboxing
In C#, boxing and unboxing are two related concepts related to treating values and expressions. Understanding these concepts is important for effective memory management and optimal performance in .NET applications.
Boxing
Boxing is the process of converting a value type (such as int, float, struct, etc.) to a reference type (specifically, an object). Create an object on this heap and copy the value to this other object.
Syntax-
Here is, the integer
num
is boxed into an objectobj
.Unboxing
Unboxing is the opposite of boxing. This involves converting the boxed product back to its corresponding value. This requires an explicit cast and of course copying the value back to a variable value type.
Syntax-
Here is, The object
obj
unboxed into an integernum
Example-
Output-
Key Points
Boxing
Unboxing
Performance Considerations
Boxing unboxing can have important business implications because,
Avoiding Boxing and Unboxing
Consider the following methods to avoid unnecessary boxing and unboxing,
By carefully understanding and managing boxing and unboxing, you can write more efficient and better-performing C# code.
Also, Read: How to sort object arrays by specific property in C#?