當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# UInt16.CompareTo()用法及代碼示例


UInt16.CompareTo方法用於將當前實例與指定對象或另一個UInt16實例進行比較。它返回一個整數,該整數顯示當前實例的值是小於,等於還是大於指定對象或另一個UInt16實例的值。此方法的重載列表中有2種方法,如下所示:

    • CompareTo(UInt16)方法
    • CompareTo(Object)方法

UInt16.CompareTo(UInt16)方法

此方法用於將當前實例與指定的16位無符號整數進行比較,並返回一個整數,該整數顯示當前實例的值是否小於,等於或大於指定的16位無符號整數的值。


用法:

public int CompareTo (ushort value);

在這裏,它需要一個無符號整數進行比較。

返回值:它返回一個32位帶符號的數字,指示當前實例和value參數的相對值,如下所示:

  • 小於零:如果當前實例
  • 零:如果當前實例=值
  • 大於零:如果當前實例>值

以下示例程序旨在說明UInt16.CompareTo(UInt16)方法的用法

示例1:

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

示例2:

// C# program to demonstrate the 
// UInt16.CompareTo(UInt16) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // calling get() method 
        get(54, 77); 
        get(70, 95); 
        get(10, 24); 
        get(45, 45); 
    } 
  
    // defining get() method 
    public static void get(ushort value1, 
                           ushort 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); 
    } 
}
輸出:
54 is less than 77
70 is less than 95
10 is less than 24
45 is equal to 45

UInt16.CompareTo(Object)方法

此方法用於將當前實例與指定對象進行比較,並返回一個整數,該整數指示當前實例的值是小於,等於還是大於對象的值。

用法:


public int CompareTo (object value);

在這裏,它需要對象與該實例進行比較,或者為null。

返回值:它返回一個32位帶符號的數字,指示當前實例和value參數的相對值,如下所示:

  • 小於零:如果當前實例
  • 零:如果當前實例=值
  • 大於零:如果當前實例>值

異常:如果值不是UInt16,則拋出ArgumentException。

以下示例程序旨在說明UInt16.CompareTo(Object)方法的用法

示例1:

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

示例2:對於ArgumentException

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

參考:



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 UInt16.CompareTo() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。