VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • C# |布尔型。比较(对象)方法

C# |布尔型。比较(对象)方法

原文:https://www . geeksforgeeks . org/c-sharp-boolean-compare tobject-method/

布尔。比较(对象)方法用于将当前实例与指定的对象进行比较,并返回一个整数,该整数显示了它们之间的关系。

语法:

public int CompareTo (object obj);

这里,需要一个对象来与当前实例或 null 进行比较。

返回值:该方法返回一个有符号整数,表示当前实例和对象的相对顺序。

  • 小于零:如果这个实例是而 obj 是
  • 零:如果这个实例和 obj 相等(要么两者都是要么两者都是)。
  • 大于零:如果此实例为而对象为

异常:如果对象不是布尔值,它将抛出参数异常

例 1:

// C# program to demonstrate the
// Boolean.CompareTo(Object) Method
using System;

class GFG {

    // Main Method
    public static void Main()
    {
        try {

            // Declaring and initializing value1
            bool value1 = true;

            // Declaring and initializing value2
            object value2 = true;

            // using CompareTo() method
            int status = value1.CompareTo(value2);

            // checking the status
            if (status > 0)
                Console.WriteLine("{0} is greater than {1}",
                                            value1, value2);

            else if (status < 0)
                Console.WriteLine("{0} is less than {1}",
                                         value1, value2);
            else
                Console.WriteLine("{0} is equal to {1}",
                                        value1, value2);
        }

        catch (ArgumentException e) 
        {
            Console.WriteLine("value2 must be Boolean");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

Output:

True is equal to True

示例 2: 适用于参数异常

// C# program to demonstrate the
// Boolean.CompareTo(Object) Method
using System;
using System.Globalization;

class GFG {

    // Main Method
    public static void Main()
    {
        try {

            // Declaring and initializing value1
            bool value1 = true;

            // Declaring and initializing value2
            object value2 = 53554;

            // using CompareTo() method
            int status = value1.CompareTo(value2);

            // checking the status
            if (status > 0)
                Console.WriteLine("{0} is greater than {1}",
                                            value1, value2);

            else if (status < 0)
                Console.WriteLine("{0} is less than {1}",
                                         value1, value2);
            else
                Console.WriteLine("{0} is equal to {1}",
                                        value1, value2);
        }

        catch (ArgumentException e) 
        {
            Console.WriteLine("value2 must be Boolean");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

Output:

value2 must be Boolean
Exception Thrown: System.ArgumentException

参考:

  • https://docs . Microsoft . com/en-us/dotnet/API/system . boolean . compare to?视图=网络框架-4.8 # 系统 _ 布尔 _ 比较 _ 系统 _ 对象 _


相关教程