Box struct to call its implemented interface : Box Unbox « struct « C# / CSharp Tutorial






public interface IDisplay
{
   void Print();
}

public struct MyStruct : IDisplay
{
   public int x;

   public void Print()
   {
      System.Console.WriteLine( "{0}", x );
   }
}

public class MainClass
{
   static void Main()
   {
      MyStruct myval = new MyStruct();
      myval.x = 123;

      // no boxing
      myval.Print();

      // must box the value
      IDisplay printer = myval;
      printer.Print();
   }
}
123
123








6.14.Box Unbox
6.14.1.Box and unbox a struct
6.14.2.Box a struct, change its value and unbox it
6.14.3.Box struct to call its implemented interface