I often get confused with boxing and unboxing. I mean I understand that both of them mean casting from System.Object to a value type which inherits from it (e.g. System.Int32 or System.Double) or contrariwise. And casting from System.Object to a value type is unboxing, like
object o = 12; int i = (int)o;
and casting from a value type to System.Object is boxing, like
long l = 123L; object o = l;
But is it boxing or unboxing in this situation?
public class Program
{
public static void V(object o) => System.Console.WriteLine(o);
public static void Main(string[] args)
{
int i = 32;
V(i);
}
}
Boxing occurs because you are converting from a value type to a reference type. When you convert to object, you need to package the integer value into an object, so it can be stored on the heap.
You can use an analyzer like the ClrHeapAllocationAnalyzer to warn you in Visual Studio when boxing occurs in your application.