Interfaces and Structs : Struct interface : Struct C# Examples


C# Examples » Struct » Struct interface »

 

Interfaces and Structs









    
using  System;

struct  Number:  IComparable
{
        int  value;
        
        public  Number(int  value)
        {
                this.value  =  value;
        }
        public  int  CompareTo(object  obj2)
        {
                Number  num2  =  (Number)  obj2;
                if  (value  <  num2.value)
                      return(-1);
                else  if  (value  >  num2.value)
                      return(1);
                else
                      return(0);
        }
}
class  MainClass
{
        public  static  void  Main()
        {
                Number  x  =  new  Number(3);
                Number  y  =  new  Number(4);
                
                IComparable  Ic  =  (IComparable)  x;
                Console.WriteLine("x  compared  to  y  =  {0}",  Ic.CompareTo(y));
        }
}
    
   
  
   



Output

x compared to y = -1


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Struct
» Struct interface