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.
Given below are the examples of C# Object Equals:
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.
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.
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.
The above is the detailed content of C# Object Equals. For more information, please follow other related articles on the PHP Chinese website!