C# : Boxing and Unboxing

Search for a command to run...

No comments yet. Be the first to comment.
💡 Exception = an unexpected event that occurred during program execution that disrupts the desired flow of the program. As mentioned above, exceptions disrupt the flow of the program and terminate the program abnormally. It's the developer's respo...
ASP.NET Core Configuration Guide Overview ASP.NET Core uses a layered configuration system where later sources override earlier ones for the same key. Configuration Priority Order Priority Source
Legacy Delphi applications often struggle with outdated UI/UX, performance issues, and monolithic architectures that make refactoring difficult. However, a full rewrite is not always feasible due to time, cost, and the risk of disrupting critical bus...
Entity Framework Core (EF Core) is a powerful and flexible object-relational mapping (ORM) framework for .NET applications, enabling developers to work with relational databases using strongly-typed .NET objects. It simplifies data access by abstract...

In this article we are going to cover some advance scenarios where Options pattern is very useful. We have covered the basic understanding of Options pattern into Options pattern in ASP.NET Core: Introduction article. Please go through it if not fami...

In this article we will learn about Options pattern in ASP.NET Core for dealing with application configurations. The Options pattern uses the C# classes to access and manage the configurations thus providing more flexibility and type safe approach to...

While working with C# datatypes, developers often needs to convert value types to reference type or vice-versa. The process of converting value type to reference type is termed as boxing and converting a reference type to value type is termed as unboxing.
System.Object is parent class of all the types, it means its possible to assign in any value to Object type instance. [Object = an alias of System.Object ]Boxing is the process of converting value type (int, char etc) to reference type (object). Boxing is an implicit conversion where a value type being allocated on the heap rather than the stack.
int i = 10;
object o = i; //performs boxing - implicit boxing
object o = (object)i; // explicit boxing
object o = (object)i; // explicit boxing but never required
//more practical example
List<object> mixedList = new List<object>();
mixedList.Add("First Group:"); // boxing - string to object
mixedList.Add(10); // boxing - int to object
Unboxing is explicit conversion from Object / reference type to value type. In the unboxing process, the boxed value type is unboxed from the heap and assigned to a value type that is stored on the stack. This process involves below steps
Checking that object instance being 'unboxed' is a 'boxed' value of given value type.
copying the value from object instance to value-type variable.
int i = 123; // a value type
object o = i; // boxing
int j = (int)o; // unboxing
//more practical example
List<object> mixedList = new List<object>();
mixedList.Add("First Group:"); // boxing - string to object
mixedList.Add(10);
foreach (var item in mixedList) {
// mixedList items are of object type,
// while priting, object type will be 'unboxed' to
// corresponding value-type.
Console.WriteLine(item);
}
For the unboxing of value types to succeed at run time, the item being unboxed must be a reference to an object that was previously created by boxing an instance of that value type. Attempting to unbox
nullcauses a NullReferenceException. Attempting to unbox a reference to an incompatible value type causes an InvalidCastException.
That's it for this article. Thanks for reading it and share your thoughts in comment section.