📜  C#中的UInt64.CompareTo()方法(带示例)

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

UInt64.CompareTo方法用于将当前实例与指定对象或另一个UInt64实例进行比较。它返回一个整数,该整数显示当前实例的值是小于,等于还是大于指定对象或另一个UInt64实例的值。此方法的重载列表中有2种方法,如下所示:

  • CompareTo(UInt64)方法
  • CompareTo(Object)方法

UInt64.CompareTo(UInt64)方法

此方法用于将当前实例与指定的64位无符号长整数进行比较,并返回一个整数,该整数显示当前实例的值是否小于,等于或大于指定的64位无符号长整数。长整数。

句法:

public int CompareTo (ulong value);

在这里,它需要一个无符号的long值进行比较。

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

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

下面的程序说明了UInt64.CompareTo(UInt64)方法的用法:

范例1:

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

范例2:

// C# program to demonstrate the
// UInt64.CompareTo(UInt64) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(34432425, 708343);
        get(3780, 8920);
        get(8543453910, 85345340920);
        get(7006789, 7006789);
    }
  
    // defining get() method
    public static void get(ulong value1,
                           ulong value2)
    {
  
        // 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);
    }
}
输出:
34432425 is greater than 708343
3780 is less than 8920
8543453910 is less than 85345340920
7006789 is equal to 7006789

UInt64.CompareTo(Object)方法

此方法用于将当前实例与指定对象进行比较,并返回一个整数,该整数指示当前实例的值是小于,等于还是大于对象的值。

句法:

public int CompareTo (object value);

在这里,它需要对象与该实例进行比较,或者为null。

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

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

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

下面的程序说明了UInt64.CompareTo(Object)方法的用法

范例1:

// C# program to demonstrate the
// UInt64.CompareTo(Object) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value1
            ulong value1 = 2324313420;
  
            // Declaring and initializing value2
            object value2 = (ulong)687.876;
  
            // 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 null"+
                       " or an instance of UInt32");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
2324313420 is greater than 687

示例2:对于ArgumentException

// C# program to demonstrate the
// UInt64.CompareTo(Object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value1
            ulong value1 = 104646549854;
  
            // Declaring and initializing value2
            object value2 = 1 / 6;
  
            // 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 null"+
                       " or an instance of UInt64");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
value2 must be null or an instance of UInt64
Exception Thrown: System.ArgumentException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.uint64.compareto?view=netstandard-2.1