Overload Operator Using C#

In this article we will try to understand how to overload operator == for our convenience.

Operator == is one of the most commonly used operators in .Net programming.

Just take an example.

Example 1

public static void Main()

{

          string MyText = "SukeshMarla";

          string YouText = Console.ReadLine();

          if(MyText == YouText)

          {

                   Console.WriteLine("Success");

          }

          else

          {

                   Console.WriteLine("Fail");

          }

}

It seems pretty cool as the actual output and the output we are expecting will be the same.

Example 2

1. Create a class called student as follows:
 

public class Stundet

{

          public int RollNo

          {

                   get;

                   set;

          }

          public string  Name

          {

                   get;

                   set;

          }

}


2.
 

public static void Main()

{

          Stundet objStudent1 = new Stundet();

          objStudent1.Name = "Sukesh Marla";

          objStudent1.RollNo = 1;

 

 

          Stundet objStudent2 = new Stundet();

          objStudent2.RollNo = 2;

          objStudent2.Name = "Dipal Shah";

 

 

          Stundet objStudent1Again = new Stundet();

          objStudent1Again.RollNo = 1;

          objStudent1Again.Name = "Sukesh Marla";

 

          Console.WriteLine("Comparision");

          Console.WriteLine("objStudent1==objStudent2:  " + (objStudent1 == objStudent2));

          Console.WriteLine("objStudent1==objStudent1Again:  " + (objStudent1 == objStudent1Again));

          Console.ReadKey();

}

Let's have a look at the output:

OverOpe1.jpg

What!!!! It finds that objStudent1 is not equal to objStudent2 but why is objStudent1 not equal to objStudent1Again?

Reason for not getting the expected result

It happened because == returns true if two operands refer to the same object, which in our case they are not.
 

Solution

In C# we can overload some of the operators by using static functions and the operator keyword.

Solution

Let's look at the code to do that.

Create a new function inside the Student class as follows:
 

public static bool operator ==(Stundet FirstObject, Stundet SecondObject)

{

          // Both are null or both  points to same instance means both are equal

          if(System.Object.ReferenceEquals(FirstObject, SecondObject))

          {

                                               

                   return true;

          }

 

          //one of them is not null means they are not equal

          if(((object)FirstObject == null) || ((object)SecondObject == null))

          {

                   return false;

          }

 

          return (FirstObject.RollNo==SecondObject.RollNo);

}


Now press F5.

We get the compile error:

The operator == requires a matching operator '!=' to also be defined

So define one more method as:
 

public static bool operator !=(Stundet FirstObject, Stundet SecondObject)

{

          return !(FirstObject == SecondObject);

}



Now press F5 and the result wil be:

OverOpe2.jpg

That's it, we got what is required.

Hope you enjoyed the article.
 



Just Compile LLP
Just Compile is an IT based firm based out of Mumbai. Application Development, Staffing, Training