There are varieties of operators available for use in C#. The C# language defines the behavior of these operators when used in an expression with the C# built-in data types. C# gives user the flexibility to change the default behaviour of the operators that means, user can define the behavior of many of the standard operators with his own structures and classes.


 


Suppose, for example, that you're writing a class that manages a set of records read from a database. If another piece of code has two objects of your class, it may want to write an expression that joins the records together into a third object. This sounds like an addition operation, and it seems natural for other pieces of code to write code like the following:


Records Records1;


Records Records2;


Records Records3;


Records3 = Records1 + Records2;


 


Your Records class would include a method that specifies "how objects of the class will behave when used in an expression with the addition operator. These methods are called user-defined operator implementations , and the object-oriented terminology for defining operator behavior in a class is called operator overloading .


Note: All operator overloading methods must be declared with both the static and public keywords.


 


Overloading Unary Plus /Unary Minus:


If you need to overload the unary plus operator, the following characteristics must be enjoyed by the method that is used to overload the unary plus.



  • A return type of your choice

  • 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 Prefix Increment and Decrement Operator:


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



  • A return type specifying the type of class or operator method

  • The keyword operator

  • The operator being overloaded

  • A parameter list specifying a single parameter containing the overloaded operator method


Example: Demonstrate Overloading of Unary Plus, Unary Minus, Prefix Increment, Prefix Decrement.


 


Overloading Unary Plus


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


  class ClsOverLoadUnaryPlus


    {


    public int X;


    public int Y;


 


    public static ClsOverLoadUnaryPlus operator +( ClsOverLoadUnaryPlus RValue)


        {


            ClsOverLoadUnaryPlus NewObj = new ClsOverLoadUnaryPlus ();


      if (RValue.X < 0)


         NewObj.X = -(RValue.X);


      else


        NewObj.X = RValue.X;


      if (RValue.Y < 0)


        NewObj.Y = -(RValue.Y);


      else


        NewObj.Y = RValue.Y;


      return NewObj;


        }


    }


}


 


Overloading Unary Minus


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


class ClsOverLoadingUnaryMinus


{


    public int X;


    public int Y;


    public static ClsOverLoadingUnaryMinus operator – ( ClsOverLoadingUnaryMinus RValue)


        {


        ClsOverLoadingUnaryMinus objNew = new ClsOverLoadingUnaryMinus ();


    if (RValue.X > 0)


        objNew.X = -(RValue.X);


    else


        objNew.X = RValue.X;


    if (RValue.Y > 0)


        objNew.Y = -(RValue.Y);


    else


        objNew.Y = RValue.Y;


    return objNew;


        }


    }


}


 


Overloading Prefix Increment


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


class ClsOverloadingIncrement


{


    public int X;


    public int Y;


 


    public static ClsOverloadingIncrement operator ++( ClsOverloadingIncrement RValue)


        {


        ClsOverloadingIncrement objIncrement = new ClsOverloadingIncrement ();


    objIncrement.X = RValue.X + 1;


    objIncrement.Y = RValue.Y + 1;


    return objIncrement;


        }


    }


}


 


Overloading Prefix Decrement


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


class ClsOverLoadingDecrement


{


    public int X;


    public int Y;


    public static ClsOverLoadingDecrement operator – ( ClsOverLoadingDecrement RValue)


        {


        ClsOverLoadingDecrement objDecrement = new ClsOverLoadingDecrement ();


    objDecrement.X = RValue.X - 1;


    objDecrement.Y = RValue.Y - 1;


    return objDecrement;


        }


    }


}


 


 


Form2.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 Form2 : Form


{


    public Form2()


    {


    InitializeComponent();


    }


 


    private void button1_Click( object sender, EventArgs e)


        { //Overloading Unary Plus


        ClsOverLoadUnaryPlus objUnaryPlus = new ClsOverLoadUnaryPlus ();


    objUnaryPlus.X = -100;


    objUnaryPlus.Y = 200;


    MessageBox.Show((objUnaryPlus.X).ToString());


    MessageBox.Show((objUnaryPlus.Y).ToString());


    objUnaryPlus = +objUnaryPlus;


    MessageBox.Show((objUnaryPlus.X).ToString());


    MessageBox.Show((objUnaryPlus.Y).ToString());


        }


 


    private void button2_Click( object sender, EventArgs e)


        { //Overloading Unary Minus


        ClsOverLoadingUnaryMinus objUnaryMinus = new ClsOverLoadingUnaryMinus ();


    objUnaryMinus.X = -10;


    objUnaryMinus.Y = 20;


    MessageBox.Show((objUnaryMinus.X).ToString());


    MessageBox.Show((objUnaryMinus.Y).ToString());


    objUnaryMinus = -objUnaryMinus;


    MessageBox.Show((objUnaryMinus.X).ToString());


    MessageBox.Show((objUnaryMinus.Y).ToString());


        }


 


    private void button3_Click( object sender, EventArgs e)


        { //Overlading Prefix Increment


        ClsOverloadingIncrement objIncrement = new ClsOverloadingIncrement ();


    objIncrement.X = 100;


    objIncrement.Y = 200;


    MessageBox.Show((objIncrement.X).ToString());


    MessageBox.Show((objIncrement.Y).ToString());


    objIncrement = ++objIncrement;


    MessageBox.Show((objIncrement.X).ToString());


    MessageBox.Show((objIncrement.Y).ToString());


        }


 


    private void button4_Click( object sender, EventArgs e)


        { //Overloading Prefix Decrement


        ClsOverLoadingDecrement objDecrement = new ClsOverLoadingDecrement ();


    objDecrement.X = 100;


    objDecrement.Y = 200;


    MessageBox.Show((objDecrement.X).ToString());


    MessageBox.Show((objDecrement.Y).ToString());


    objDecrement = --objDecrement;


    MessageBox.Show((objDecrement.X).ToString());


    MessageBox.Show((objDecrement.Y).ToString());


        }


    }


}


 


 


Output:


 


 





 


Click on Overloading Unary Plus:


   


Click on Overloading Unary Minus:


   


Click on Prefix Increment:


  


Click on Prefix Decrement:


  


 

                    

Copyright © 2010 VisualBuilder. All rights reserved