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

Equals( ) is a virtual method belonging to Object. Its default behavior is to check for the values of the compared variables and return true if their values are same.

However C# gives you the provision to override this method and overload the method as well. In this article you will understand the default behavior of Equals( ) method and you will also come to know how to override this method.

Equals( ) method is meant for value equality. Consider the following example:

class sampleClass {
public static void Main() {
string str1 = “Hello”;
string str2 = “Hai”;
string str3 = “Hai”;
if(str1.Equals(“Hello”)) {
Console.WriteLine(“str1 is Hello”);
}
if(str2.equals(str3)) {
Console.WriteLine(“str2 and str3 are equal. Their value is:”, str3);
}
}
}

Output of this code will be:

str1 is Hello
str2 and str3 are equal. Their value is Hai

Though Equals() performs an operation, it is not an operator. It is a method. Hence you can override the method and give a new meaning for it. You are still allowed to overload the method with multiple definitions in your class. Here is an example overriding Equals() method from the base class Object. It also overloads the function to perform two different activities:

class sampleClass {
int member1;
public sampleClass(int member1) {
this.member1 = member1;
}
public override bool Equals(System.Object obj) {
bool result = false;
sampleClass obj1 = obj as sampleClass;
if((System.Object) obj1 != null) {
result = (this == (sampleClass)obj1);
}
return result;
}
public override bool Equals(sampleClass obj) {
bool result = (this == (sampleClass)obj);
return result;
}
}
class testClass {
sampleClass instance1 = new sampleClass(10);
sampleClass instance2 = new sampleClass(10);
sampleClass instance3 = instance1;
System.Object instance4 = new System.Object();
if(instance1.Equals(instance2)) {
Console.WriteLine(“instance2 is equal to instance1”);
}
if(instance1.Equals(instance3)) {
Console.WriteLine(“instance3 is equal to instance1”);
}
if(instance4.Equals(instance1)) {
Console.WriteLine(“instance4 is equal to instance1”);
}
}

Output of this code will be:

instance3 is equal to instance1

In this example, you override Equals() method in sampleClass and overload it to accept System.Object as parameter in one method and accept sampleClass object as parameter to the other overloaded method.

You have overridden and overloaded the methods to make them check for referential equality instead of value equality. i.e. you check if the objects are referencing to same instance instead of checking if both the objects hold same value for its members.

| 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.