How Do You Overload == Operator in C# (C Sharp) ?

C# supports operator overloading for a particular set of operators. One among them is the comparison operator “==“. This operator is used for performing equality function and it is also termed as referential equality.



When you compare two objects using == operator, it checks if both the objects are just two object references pointing to the same object. If so it returns true else it returns false. If the two objects are references to different objects but share the same value, then again reference equality will be false.

Here is an example of == operator:
class sampleClass {
int member1, member2;
public sampleClass(int member1, int member2) {
this.member1 = member1;
this.member2 = member2;
}
}
class testClass {
public static void Main() {
sampleClass obj1 = new sampleClass(50,100);
sampleClass obj2 = new sampleClass(50,100);
sampleClass obj3 = obj1;
if(obj1 == obj2) {
Console.WriteLine(“obj1 and obj2 are referentially equal”);
}
if(obj1 == obj3) {
Console.WriteLine(“obj1 and obj3 are referentially equal”);
}
if(obj2 == obj3) {
Console.WriteLine(“obj2 and obj3 are referentially equal”);
}
}
}

Output of this code will be:

obj1 and obj3 are referentially equal

In this example, members of obj1, obj2 and obj3 have the same value. But only obj1 and obj3 are referentially equal. This is because obj1 and obj2 are two different instances of sampleClass created using new operator. The object obj3 is not newly created using new operator. It is referencing to the same object obj1 using the statement obj3 = obj1. Hence they are referentially equal.

C# gives you an option to overload this == operator. By overloading you can change its meaning. But you have a restriction, if you overload the comparison operator == then you should overload the comparison operator != and vice versa. To understand how to overload == operator; consider the scenario given below along with its code sample.

As already discussed, == operator checks for reference equality. When two objects are referentially equal then it is obvious that they share the same value. But when two objects share the same value, it is not necessary that they are referentially equal.

Now you are going to overload this operator to return true if the values of two objects of the same class are equal. Overloaded == operator is not going to check if objects hold the same references. This overloading is demonstrated in the code sample below:

class sampleClass {
int member1, member2;
public sampleClass(int member1, int member2) {
this.member1 = member1;
this.member2 = member2;
}
public static bool operator ==(sampleClass obj1, sampleClass obj2) {
bool result = false;
if (obj1.member1 == obj2.member1 && obj1.member2 == obj2.member2) {
result = true;
}
return result;
}
public static bool operator != (sampleClass obj1, sampleClass obj2) {
return !( obj1== obj2);
}
public override bool Equals(object obj) {
bool result = false;
if (obj is sampleClass) {
result = (this == (sampleClass)obj);
}
return result;
}
public override int GetHashCode() {
return (member1 * member2).GetHashCode();
}
}
class testClass {
public static void Main() {
sampleClass obj1 = new sampleClass(50,100);
sampleClass obj2 = new sampleClass(50,100);
sampleClass obj3 = obj1;
if(obj1 == obj2) {
Console.WriteLine(“obj1 and obj2 are referentially equal”);
}
if(obj1 == obj3) {
Console.WriteLine(“obj1 and obj3 are referentially equal”);
}
if(obj2 == obj3) {
Console.WriteLine(“obj2 and obj3 are referentially equal”);
}
}
}

Output of this code will be:

obj1 and obj2 are referentially equal
obj1 and obj3 are referentially equal
obj2 and obj3 are referentially equal

You might be surprised with the output of this example when compared to the earlier example. In this example obj1 and obj2 are two different objects of sampleClass created using new operator. Members of both these objects share the same value.

In normal scenario, obj1 == obj2 will surely fail. But in this example, you have overloaded ==operator to check for value of the members instead of the object references. Hence obj1==obj2 returns true. Similarly obj2 == obj3 will return true. obj1 is reference to the object obj3. Hence they always share the same value as that of obj3 and hence they also return true.

Note that in the above example, you have also overloaded != operator since comparison operators should be overloaded in pairs. In addition, you have overridden the Equals method and GetHashCode method.

| How Do You Establish Polymorphism in C# (C Sharp) | How Do You Overload == Operator in C# (C Sharp) ? | How Do You Overload == Operator in C# (C Sharp) ? | How to Perform User Defined Conversions Between Structures (Struct) | Illustration of C# (C Sharp) Keywords (params, ref, out) Used to Declare | Introduction to Static Classes of C# (C Sharp) | Introduction to Static Members of C# ( C Sharp) | Overview of Structs in C# (C Sharp) | Using Constructors and Destructors in C# (C Sharp) |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.