当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Boolean.Equals(Boolean)用法及代码示例


此方法用于返回一个值,该值指示此实例是否等于指定的布尔对象。

用法:

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<T>接口,并且比Equals稍好一些,因为它不必将obj参数转换为对象。

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Boolean.Equals(Boolean) Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。