此方法用于返回一个值,该值指示此实例是否等于指定的布尔对象。
句法:
public bool Equals (bool obj);
在这里, obj是一个布尔值,用于与此实例进行比较。
返回值:如果obj与该实例具有相同的值,则此方法返回true ,否则返回false 。
下面的程序说明了Boolean.Equals(bool obj)方法的用法:
范例1:
// C# program to demonstrate
// Boolean.Parse(String)
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
// passing different values
// to the method to check
check(true, true);
check(true, false);
check(false, true);
check(false, false);
}
// Defining check method
public static void check(bool input1, bool input2)
{
// declaring bool variable
bool val;
// getting parsed value
val = input1.Equals(input2);
// cheking the equivalency
if (val == true)
Console.WriteLine("{0} is equal to {1}",
input1, input2);
else
Console.WriteLine("{0} is not equal to {1}",
input1, input2);
}
}
输出:
True is equal to True
True is not equal to False
False is not equal to True
False is equal to False
范例2:
// C# program to demonstrate
// Boolean.Parse(String)
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
// Declaring the variable
// input1 and input2
bool input1, input2;
// initializing the variables
input1 = true;
input2 = false;
// cheking the equality
bool val = input1.Equals(input2);
// cheking the equivalency
if (val == true)
Console.WriteLine("input1 is equal to input2");
else
Console.WriteLine("input1 is not equal to input2");
}
}
输出:
input1 is not equal to input2
注意:此方法实现System.IEquatable
接口,并且比Equals稍好一些,因为它不必将obj参数转换为对象。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.boolean.equals?view=netframework-4.7.2#System_Boolean_Equals_System_Boolean_