当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Single.CompareTo()用法及代码示例


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

    • CompareTo(Single)方法
    • CompareTo(Object)方法

Single.CompareTo(Single) Method

此方法用于将当前实例与指定的单精度浮点数进行比较,并返回一个整数,该整数指示此实例的值是否小于,等于或大于指定的单精度浮点的值。点数。


用法:

public int CompareTo (float value);

在这里,需要一个单精度浮点数进行比较。

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

  • 小于零:如果当前实例
  • 零:如果Current Instance = value或当前实例和value都不是数字(NaN),PositiveInfinity或NegativeInfinity。
  • 大于零:如果当前实例>值,或者当前实例是一个数字,而值不是一个数字(NaN)。

以下示例程序旨在说明Single.CompareTo(Single)方法的用法:

示例1:

// C# program to demonstrate the 
// Single.CompareTo(Single) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Declaring and initializing value1 
        float value1 = 10.5f; 
  
        // Declaring and initializing value2 
        float value2 = 20.6f; 
  
        // compare both the values 
        // 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.5 is less than 20.6

示例2:

// C# program to demonstrate the 
// Single.CompareTo(Single) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // calling get() method 
        get(5.4f, 7.5f); 
        get(30.4f, 20.3f); 
        get(10.4f, 10.4f); 
        get(7.2f, -12.3f); 
    } 
  
    // defining get() method 
    public static void get(float value1, 
                           float 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); 
    } 
}
输出:
5.4 is less than 7.5
30.4 is greater than 20.3
10.4 is equal to 10.4
7.2 is greater than -12.3

Single.CompareTo(Object) Method

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

用法:


public int CompareTo (object value);

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

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

  • 小于零:如果当前实例
  • 零:如果Current Instance = value或当前实例和value都不是数字(NaN),PositiveInfinity或NegativeInfinity。
  • 大于零:如果当前实例>值,或者当前实例是一个数字,而值不是一个数字(NaN)。

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

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

示例1:

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

示例2:对于ArgumentException

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

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Single.CompareTo() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。