📜  C#中的Int32.CompareTo方法与示例

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

Int32.CompareTo方法用于将当前实例与指定的对象或Int32进行比较,并返回其相对值的符号。此方法的重载列表中有2种方法,如下所示:

  • CompareTo(Int32)方法
  • CompareTo(Object)方法

Int32.CompareTo(Int32)方法

此方法用于将当前实例与指定的32位带符号整数进行比较,并返回其相对值的符号。

句法:

public int CompareTo (int value);

在这里,需要整数进行比较。

返回值:返回一个32位带符号的数字,指示当前实例和value参数的相对值,如下所示:

  • 小于零:如果当前实例<值
  • 零:如果当前实例=值
  • 大于零:如果当前实例>值

下面的程序说明了上述方法的用法:

范例1:

// C# program to demonstrate the
// Int32.CompareTo(Int32) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring and initializing value1
        int value1 = 10;
  
        // Declaring and initializing value2
        int value2 = 20;
  
        // 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);
    }
}
输出:
10 is less than 20

范例2:

// C# program to demonstrate the
// Int32.CompareTo(Int32) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(5, 7);
        get(3025, 3025);
        get(10, 20);
        get(7, -12);
    }
  
    // defining get() method
    public static void get(int value1,
                           int value2)
    {
  
        // using the 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);
    }
}
输出:
5 is less than 7
3025 is equal to 3025
10 is less than 20
7 is greater than -12

Int32.CompareTo(Object)方法

此方法用于将当前实例与指定对象进行比较,并返回其相对值的符号。

句法:

public int CompareTo (object value);

在这里,它需要一个对象进行比较,或者为null。

返回值:返回一个32位带符号的数字,指示当前实例和value参数的相对值,如下所示:

  • 小于零:如果当前实例<值
  • 零:如果当前实例=值
  • 大于零:如果当前实例> valuevalue为null。

异常:如果value不是Int32 ,则抛出ArgumentException

下面的程序说明了上述方法的用法:

范例1:

// C# program to demonstrate the
// Int32.CompareTo(Object) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value1
            int value1 = 10;
  
            // Declaring and initializing value2
            object value2 = 562587;
  
            // 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 Int32");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
10 is less than 562587

示例2:对于ArgumentException

// C# program to demonstrate the
// Int32.CompareTo(object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value1
            int value1 = 10;
  
            // Declaring and initializing value2
            object value2 = 9856745963;
  
            // 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 Int32");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
value2 must be Int32
Exception Thrown: System.ArgumentException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.int32.compareto?view=netframework-4.7.2