Box a struct, change its value and unbox it : Box Unbox « struct « C# / CSharp Tutorial






public interface IModifyMyStruct
{
   int X
   {
      get;
      set;
   }
}

public struct MyStruct : IModifyMyStruct
{
   public int x;

   public int X
   {
      get
      {
         return x;
      }

      set
      {
         x = value;
      }
   }

   public override string ToString()
   {
      return x.ToString();
   }
}

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

      object obj = myval;
      System.Console.WriteLine( "{0}", obj.ToString() );

      IModifyMyStruct iface = (IModifyMyStruct) obj;
      iface.X = 456;
      System.Console.WriteLine( "{0}", obj.ToString() );

      MyStruct newval = (MyStruct) obj;
      System.Console.WriteLine( "{0}", newval.ToString() );
   }
}
123
456
456








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