Operator Overloading in C# -2

text zoom

Overloading Boolean Operator:


If you need to overload the true or false operators in your class or structure, define a method with the following characteristics:



  • A return type of bool

  • The keyword operator

  • The operator being overloaded

  • A parameter list specifying a single parameter of the type of class or structure containing the overloaded operator method


Overloading Conversion Operator:


You can also write operator overload methods that convert one type into another type. If you need to define a new conversion operator in your class or structure, define a method with the following characteristics:



  • The keyword implicit if the conversion is to be treated as an implicit conversion, or the keyword explicit if the conversion is to be treated as an explicit conversion

  • The keyword operator

  • A type specifying the target type of the conversion

  • A parameter list specifying the source type of the conversion


 


Overloading Binary Operators:


The operators that fall under this category are : Addition, Subtraction, Division, Multiplication, Remainder, AND, OR, Exclusive OR, Shift left, Shift Right, Equality, Inequality, Greater than, less than. If you need to overload any of the binary operators in your class or structure, define a method with the following characteristics:



  • A return type of your choice

  • The keyword operator

  • The operator being overloaded

  • A parameter list specifying two parameters, at least one of which must be of the type of class or structure containing the overloaded operator method


 


Example: Demonstrate Boolean Overloading, Conversion Overloading and Binary Operators Overloading


 


ClsOverloadingBooleanOpertors.cs


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


  class ClsOverloadingBooleanOpertors


    {


    public int X;


    public int Y;


 


    public static bool operator true ( ClsOverloadingBooleanOpertors RValue)


        {


    if ((RValue.X == 0) && (RValue.Y == 0))


        return true ;


    return false ;


        }


    public static bool operator false ( ClsOverloadingBooleanOpertors RValue)


        {


    if ((RValue.X == 0) && (RValue.Y == 0))


        return false ;


    return true ;


        }


    }


}


 


ClsOverloadingConversion.cs


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


  class ClsOverloadingConversion


    {


    public int X;


    public int Y;


    public static implicit operator double ( ClsOverloadingConversion RValue)


        {


    double total_Distance;


    double total_Sum;


        total_Sum = (RValue.X * RValue.X) + (RValue.Y * RValue.Y);


        total_Distance = System. Math .Sqrt(total_Sum);


    return total_Distance;


        }


    }


}


 


ClsOverloadingEqualityInequality.cs


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


  class ClsOverloadingEqualityInequality


    {


    public int X;


    public int Y;


        public static bool operator ==( ClsOverloadingEqualityInequality objFirst,         ClsOverloadingEqualityInequality objSecond)


        {


    if (objFirst.X != objSecond.X)


        return false ;


    if (objFirst.Y != objSecond.Y)


        return false ;


    return true ;


        }


    public override bool Equals( object o)


        {


        return true ;


        }


    public override int GetHashCode()


        {


        return 0;


        }


    public static bool operator !=( ClsOverloadingEqualityInequality objFirst,     ClsOverloadingEqualityInequality objSecond)


        {


    if (objFirst.X != objSecond.X)


        return true ;


    if (objFirst.Y != objSecond.Y)


        return true ;


    return false ;


        }


    }


}


 


Form3.cs


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


 


namespace _CSharpApplication


{


  public partial class Form3 : Form


    {


    public Form3()


        {


    InitializeComponent();


        }


 


    private void button1_Click( object sender, EventArgs e)


        { //Overloading True and False operator


            ClsOverloadingBooleanOpertors objBoolean = new ClsOverloadingBooleanOpertors ();


      objBoolean.X = 20;


      objBoolean.Y = 30;


      if (objBoolean)


        MessageBox.Show( "The point is at the origin." );


      else


        MessageBox.Show( "The point is not at the origin." );


        }


 


    private void button2_Click( object sender, EventArgs e)


        { //Overloadable Conversion Operator


            double conDistance;


            ClsOverloadingConversion objConversion = new ClsOverloadingConversion ();


      objConversion.X = 100;


      objConversion.Y = 200;


            conDistance = objConversion;


      MessageBox.Show(conDistance.ToString());


        }


 


    private void button3_Click( object sender, EventArgs e)


        { //Overloading Equality/Inequality Operator


            ClsOverloadingEqualityInequality MyFirstPoint = new ClsOverloadingEqualityInequality ();


            ClsOverloadingEqualityInequality MySecondPoint = new ClsOverloadingEqualityInequality ();


            ClsOverloadingEqualityInequality MyThirdPoint = new ClsOverloadingEqualityInequality ();


      MyFirstPoint.X = 100;


      MyFirstPoint.Y = 200;


      MySecondPoint.X = 500;


      MySecondPoint.Y = 750;


      MyThirdPoint.X = 100;


      MyThirdPoint.Y = 200;


      if (MyFirstPoint == MySecondPoint)


        MessageBox.Show( "MyFirstPoint and MySecondPoint are at the same coordinates." );


      else


        MessageBox.Show( "MyFirstPoint and MySecondPoint are not at the same coordinates." );


      if (MyFirstPoint == MyThirdPoint)


        MessageBox.Show( "MyFirstPoint and MyThirdPoint are at the same coordinates." );


      else


        MessageBox.Show( "MyFirstPoint and MyThirdPoint are not at the same coordinates." );


 


        }


    }


}


 


Output:


 





 


Clicking on Overloading True/False Operator





Clicking on Overloading Conversion Operator





 


Clicking on Overloading Equality/Inequality Operator





 





 


 

                    

Copyright © 2009 VisualBuilder. All rights reserved