C# Object Equals

WBOY
Release: 2024-09-03 15:06:02
Original
251 people have browsed it

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);
Copy after login

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);
}
}
}
Copy after login

Output:

C# Object Equals

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);
}
}
}
Copy after login

Output:

C# Object Equals

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);
}
}
}
Copy after login

Output:

C# Object Equals

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!