Box a struct, change its value and unbox it : Box Unbox : Struct C# Examples


C# Examples » Struct » Box Unbox »

 

Box a struct, change its value and unbox it









    
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()  );
      }
}
    
   
  
   



Output

123
456
456


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Struct
» Box Unbox