Updated April 7, 2023
Introduction to C# Object Equals
To compare if the current instance is equal to a given object or not, we make use of a function called Equals(Object) function in C#, which takes the object that is to be compared with its current instance as the parameter and returns true if the object that is be compared with its current instance as the parameter is same as the current instance and returns false if the object that is be compared with its current instance as the parameter is not same as the current instance and this function is available in .NET 5.0 version and other 35 versions.
Syntax to Declare Equals() Function:
Equals(Object);
Where an object is an object that is be compared with its current instance.
Working of Equals(Object) Function
- To compare if the current instance is equal to a given object or not, we make use of a function called Equals(Object) function in C#.
- The Equals(Object) function takes the object that is to be compared with its current instance as the parameter.
- The Equals(Object) function returns true if the object that is be compared with its current instance as the parameter is the same as the current instance.
- The Equals(Object) function returns false if the object that is be compared with its current instance as the parameter is not the same as the current instance.
- The Equals(Object) function is available in .NET 5.0 version and other 35 versions.
Examples of C# Object Equals
Given below are the examples of C# Object Equals:
Example #1
C# program to create an object and pass it as the parameter to the Equals(Object) function to compare it with its current instance and display the result.
Code:
using System.IO;
using System;
public class check
{
public static void Main()
{
//declaring a Boolean variable called firstvalue and storing a Boolean value in it
bool firstvalue = false;
//declaring an object called secondvalue and storing an expression in it
object secondvalue = 1 * 0;
//using Equals(object) method to compare the firstvalue with the current instance secondvalue and storing the result in a variable called result
bool result = firstvalue.Equals(secondvalue);
//based on the result, displaying if the firstvalue is equal to the secondvalue or not
if (result)
{
Console.WriteLine("The first value {0} is equal to the second value {1}", firstvalue, secondvalue);
}
else
{
Console.WriteLine("The first value {0} is not equal to the second value {1}", firstvalue, secondvalue);
}
}
}
Output:
In the above program, we define a Boolean variable called firstvalue and store a Boolean value in it. Then we are defining an object called secondvalue and storing an expression in it. Then we are making use of the Equals(Object) function to compare the value stored in the firstvalue variable and the value stored in the secondvalue object and then store its result in a variable called the result variable. Then using the if condition statement, based on the result obtained, we are displaying if the firstvalue is equal to the secondvalue or not.
Example #2
C# program to create an object and pass it as the parameter to the Equals(Object) function to compare it with its current instance and display the result.
Code:
using System.IO;
using System;
public class check
{
public static void Main()
{
//declaring a Boolean variable called firstvalue and storing a Boolean value in it
bool firstvalue = true;
//declaring an object called secondvalue and storing an expression in it
object secondvalue = 10 /10;
//using Equals(object) method to compare the firstvalue with the current instance secondvalue and storing the result in a variable called result
bool result = firstvalue.Equals(secondvalue);
//based on the result, displaying if the firstvalue is equal to the secondvalue or not
if (result)
{
Console.WriteLine("The first value {0} is equal to the second value {1}", firstvalue, secondvalue);
}
else
{
Console.WriteLine("The first value {0} is not equal to the second value {1}", firstvalue, secondvalue);
}
}
}
Output:
In the above program, we define a Boolean variable called firstvalue and store a Boolean value in it. Then we are defining an object called secondvalue and storing an expression in it. Then we are making use of the Equals(Object) function to compare the value stored in the firstvalue variable and the value stored in the secondvalue object and then store its result in a variable called the result variable. Then using the if condition statement, based on the result obtained, we are displaying if the firstvalue is equal to the secondvalue or not.
Example #3
C# program to create an object and pass it as the parameter to the Equals(Object) function to compare it with its current instance and display the result.
Code:
using System.IO;
using System;
public class check
{
public static void Main()
{
//declaring a Boolean variable called firstvalue and storing a Boolean value in it
bool firstvalue = true;
//declaring an object called secondvalue and storing an expression in it
object secondvalue = 10 - 5;
//using Equals(object) method to compare the firstvalue with the current instance secondvalue and storing the result in a variable called result
bool result = firstvalue.Equals(secondvalue);
//based on the result, displaying if the firstvalue is equal to the secondvalue or not
if (result)
{
Console.WriteLine("The first value {0} is equal to the second value {1}", firstvalue, secondvalue);
}
else
{
Console.WriteLine("The first value {0} is not equal to the second value {1}", firstvalue, secondvalue);
}
}
}
Output:
In the above program, we define a Boolean variable called firstvalue and store a Boolean value in it. Then we are defining an object called secondvalue and storing an expression in it. Then we are making use of the Equals(Object) function to compare the value stored in the firstvalue variable and the value stored in the secondvalue object and then store its result in a variable called the result variable. Then using the if condition statement, based on the result obtained, we are displaying if the firstvalue is equal to the secondvalue or not.
Recommended Articles
This is a guide to C# Object Equals. Here we discuss the introduction, working of equals(object) function and examples, respectively. You may also have a look at the following articles to learn more –