首页  >  文章  >  后端开发  >  C# 对象等于

C# 对象等于

WBOY
WBOY原创
2024-09-03 15:06:02200浏览

为了比较当前实例是否等于给定对象,我们使用 C# 中名为 Equals(Object) 的函数,该函数将要与其当前实例进行比较的对象作为参数,如果作为参数与其当前实例进行比较的对象与当前实例相同,则返回 true;如果作为参数与其当前实例进行比较的对象与当前实例不同,则返回 false,且该函数为提供.NET 5.0版本和其他35个版本。

声明 Equals() 函数的语法:

Equals(Object);

其中对象是与其当前实例进行比较的对象。

Equals(Object) 函数的工作

  • 为了比较当前实例是否等于给定对象,我们使用 C# 中名为 Equals(Object) 的函数。
  • Equals(Object) 函数将要与其当前实例进行比较的对象作为参数。
  • 如果作为参数与其当前实例进行比较的对象与当前实例相同,则 Equals(Object) 函数返回 true。
  • 如果作为参数与其当前实例进行比较的对象与当前实例不同,Equals(Object) 函数将返回 false。
  • Equals(Object)函数在.NET 5.0版本和其他35个版本中可用。

C# 对象等于的示例

下面给出了 C# 对象等于的示例:

示例#1

C# 程序创建一个对象并将其作为参数传递给 Equals(Object) 函数,以将其与其当前实例进行比较并显示结果。

代码:

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);
}
}
}

输出:

C# 对象等于

在上面的程序中,我们定义了一个名为firstvalue的布尔变量,并在其中存储了一个布尔值。然后我们定义一个名为 SecondValue 的对象并在其中存储一个表达式。然后,我们使用 Equals(Object) 函数来比较存储在第一个值变量中的值和存储在第二个值对象中的值,然后将其结果存储在一个称为结果变量的变量中。然后使用 if 条件语句,根据获得的结果,显示第一个值是否等于第二个值。

示例#2

C# 程序创建一个对象并将其作为参数传递给 Equals(Object) 函数,以将其与其当前实例进行比较并显示结果。

代码:

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);
}
}
}

输出:

C# 对象等于

在上面的程序中,我们定义了一个名为firstvalue的布尔变量,并在其中存储了一个布尔值。然后我们定义一个名为 SecondValue 的对象并在其中存储一个表达式。然后,我们使用 Equals(Object) 函数来比较存储在第一个值变量中的值和存储在第二个值对象中的值,然后将其结果存储在一个称为结果变量的变量中。然后使用 if 条件语句,根据获得的结果,显示第一个值是否等于第二个值。

示例#3

C# 程序创建一个对象并将其作为参数传递给 Equals(Object) 函数,以将其与其当前实例进行比较并显示结果。

代码:

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);
}
}
}

输出:

C# 对象等于

在上面的程序中,我们定义了一个名为firstvalue的布尔变量,并在其中存储了一个布尔值。然后我们定义一个名为 SecondValue 的对象并在其中存储一个表达式。然后,我们使用 Equals(Object) 函数来比较存储在第一个值变量中的值和存储在第二个值对象中的值,然后将其结果存储在一个称为结果变量的变量中。然后使用 if 条件语句,根据获得的结果,显示第一个值是否等于第二个值。

以上是C# 对象等于的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn