📜  C#| Boolean.CompareTo(Boolean)方法

📅  最后修改于: 2021-05-29 19:37:21             🧑  作者: Mango

Boolean.CompareTo(Boolean)方法用于将当前实例与指定的布尔对象进行比较,并返回其相对值的指示。

句法:

public int CompareTo (bool value);

在此,该是要与当前实例进行比较的布尔对象。

返回值:该方法返回一个32位带符号整数,该整数指示此实例和value的相对顺序。

  • 小于零:如果此实例为false ,则值为true
  • 零:如果此实例和值相等(都为true或均为false )。
  • 大于零:如果此实例为true ,则值为false

例子:

// C# program to demonstrate
// Boolean.CompareTo(Boolean)
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring val1 and val2
        bool val1, val2;
  
        // intializing the val1,
        // and val2
        val1 = true;
        val2 = false;
  
        // using CompareTo method
        int i = val2.CompareTo(val1);
  
        // checking the condition
        if (i > 0)
            Console.Write("val2 is greater than val1");
  
        else if (i < 0)
            Console.Write("val2 is less than val1");
  
        else
            Console.Write("val1 is equal to val1");
    }
}
输出:
val2 is less than val1

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.boolean.compareto?view=netframework-4.8#System_Boolean_CompareTo_System_Boolean_