Introduction
In some cases of real development time, we are required to check the equality, either its for value or reference.
When we compare two string values or any primitive type, then it means we are going to test value equality. This is also known as equivalence.
When we will compare two reference types, (string, object, class), then it means we are going to compare its reference equality.
In this article, I will explain both.
As we know, in C# we have 2 types:
- Value Type
- Reference Type
Based on that, C# provides 3 things to compare value type and reference type:
- ==,
- Equal()
- ReferenceEquals()
== operator
It is used to compare object reference equality. If the type is reference, it is by default, but when the value is immutable, it means its value will not be changed. Then it's overloading the operator and comparing its value.
ReferenceEquals()
If we required to check for reference then we could use ReferenceEquals.
It is a static method and takes two parameters both must be a reference type.
Also, it is not throwing any exception kind of a NullReferenceException
public static bool ReferenceEquals(Object? objA, Object? objB);
Equal()
It is a C# method that returns a boolean value based on compare
public virtual bool Equals (object? obj);
public static bool Equals(Object? objA, Object? objB);
- object o1 = null;
- object o2 = new object();
- Console.WriteLine("object o1 = null;");
- Console.WriteLine(" object o2 = new object();");
-
-
- Console.WriteLine("ReferenceEquals(o1, o1) : ", ReferenceEquals(o1, o1));
-
-
- Console.WriteLine("ReferenceEquals(o2, o2): ", ReferenceEquals(o2, o2));
-
-
- Console.WriteLine("ReferenceEquals(o1, o2): ",ReferenceEquals(o1, o2));
-
-
- Console.WriteLine("ReferenceEquals(o2, o1): ", ReferenceEquals(o2, o1));
-
-
- Console.WriteLine("o1.Equals(o1): ", o1.Equals(o1));
-
-
- Console.WriteLine("o1.Equals(o2);: ", o1.Equals(o2));
-
-
- Console.WriteLine("o2.Equals(o1): ", o2.Equals(o1));
-
-
- Console.WriteLine("o2.Equals(o2): ", o2.Equals(o2));
-
In this output window, you can check the result.
Now let's try with more examples for clarification.
== operator is used to check the reference.
- string s1 = "test";
- string s2 = "Test";
- Console.WriteLine(s1 == s2);
- Console.ReadKey();
Equals() is used to check the content.
- string s1 = "hello";
- string s2 = str;
- Console.WriteLine("equals() result: {0}", s1.Equals(s2));
- Console.ReadKey();
ReferenceEquals checks for two reference types
- using System;
- class ReferenceEqualsTest {
- public int Age {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- static void Main() {
- var a = new ReferenceEqualsTest() {
- Age = 18, Name = "Smith"
- };
- var b = new ReferenceEqualsTest() {
- Age = 1, Name = "Smith"
- };
- bool isEqual = ReferenceEquals(a, b);
-
- System.Console.WriteLine("ReferenceEquals(a, b) = {0}", isEqual);
-
- b = a;
- isEqual = ReferenceEquals(a, b);
-
- System.Console.WriteLine("ReferenceEquals(a, b) = {0}", isEqual);
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
See that in this code, we created two objects, but after the first console print, we have assigned a into b so that both references refer to the same object.
That's the reason we get true in the output.
C# also provides Delegate equality
When we compare two delegates of the same runtime type, they are equal when both are null or their respective lists are of the same length and they have equal data in both:
Example
- class Program {
- static void Main(string[] args) {
- Action a = () => Console.WriteLine("a"); & ntbsp;
- Action b = a + a;
- Action c = a + a;
-
- Console.WriteLine(ReferenceEquals(b, c));
-
- Console.WriteLine(b == c);
- Console.ReadKey();
- }
- }
See the image for the output:
Summary
== operator behaves the same as this ReferenceEquals() method by default, but this can be overridden.
The Equals() method is used to tests for data equality it will return a boolean value.
This is a static method, and we have the overload method as well.
ReferenceEquals methods work only for reference types. The ReferenceEquals method is static.
It returns a boolean value and takes two reference type parameters.