Box struct to call its implemented interface : Box Unbox : Struct C# Examples


C# Examples » Struct » Box Unbox »

 

Box struct to call its implemented interface









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



Output

123
123


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Struct
» Box Unbox