public void Print()
{
System.Console.WriteLine( "{0}", x );
}
}
public class EntryPoint
{
static void Main()
{
MyValue myval = new MyValue();
myval.x = 123;
// no boxing
myval.Print();
// must box the value
IPrint printer = myval;
printer.Print();
}
}
The first call to Print is done through the value reference, which doesn’t incur boxing. How-
ever, the second call to Print is done through an interface. The boxing takes place at the point
where you obtain the interface. At first, it looks like you can easily sidestep the boxing operation by
not acquiring an explicit reference typed on the interface type. This is true in this case, since Print
is also part of the public contract of MyValue. However, had you implemented the Print method as
an explicit interface, which ...