Articles → CSHARP → Boxing And Unboxing In C#
Boxing And Unboxing In C#
Boxing
Unboxing
Example
using System;
namespace BoxingUnboxingDemo
{
class Program
{
static void Main(string[] args)
{
int intValue = 42;
object boxedValue = intValue; // Boxing
Console.WriteLine($"Original value: {intValue}");
Console.WriteLine($"Boxed value: {boxedValue}");
// Unboxing: Converting reference type back to value type
int unboxedValue = (int)boxedValue; // Unboxing
Console.WriteLine($"Unboxed value: {unboxedValue}");
Console.ReadLine();
}
}
}
Output
Click to Enlarge