bool operator for Complex : Bool operator : Operator Overload C# Examples


C# Examples » Operator Overload » Bool operator »

 

bool operator for Complex









    
using  System;

public  struct  Complex
{
        public  Complex(  double  real,  double  imaginary  )  {
                this.real  =  real;
                this.imaginary  =  imaginary;
        }

        public  override  string  ToString()  {
                return  String.Format(  "({0},  {1})",  real,  imaginary  );
        }

        public  double  Magnitude  {
                get  {
                        return  Math.Sqrt(  Math.Pow(this.real,  2)  +  Math.Pow(this.imaginary,  2)  );
                }
        }

        public  static  implicit  operator  bool(  Complex  c  )  {
                return  (c.real  !=  0)  ||  (c.imaginary  !=  0);
        }

        private  double  real;
        private  double  imaginary;
}

public  class  MainClass
{
        static  void  Main()  {
                Complex  cpx1  =  new  Complex(  1.0,  3.0  );
                if(  cpx1  )  {
                        Console.WriteLine(  "cpx1  is  true"  );
                }  else  {
                        Console.WriteLine(  "cpx1  is  false"  );
                }

                Complex  cpx2  =  new  Complex(  0,  0  );
                Console.WriteLine(  "cpx2  is  {0}",  cpx2  ?  "true"  :  "false"  );
        }
}
    
   
  
   



Output

cpx1 is true
cpx2 is false


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Operator Overload
» Bool operator