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


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


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

用法:

public int CompareTo (byte value);

在此,该值是要比较的8位无符号整数。


返回值:此方法返回一个带符号的整数,该整数指示此实例和值的相对顺序。

  • 小于零:此实例小于值。
  • 零:此实例等于值。
  • 大于零:此实例大于值。

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

示例1:

// C# program to demonstrate 
// Byte.CompareTo(byte) 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Declaring val1 and val2 
        byte val1, val2; 
  
        // intializing the val1,  
        // val2 and val3 
        val1 = 12; 
        val2 = 13; 
  
        // getting compared constant 
        // using CompareTo method 
        int i = val2.CompareTo(val1); 
  
        // checking the condition 
        if (i > 0) 
            Console.Write("val2 is greater than val1"); 
  
        else if (i < 0) 
            Console.Write("val2 is less than val1"); 
  
        else
            Console.Write("val1 is equal to val1"); 
    } 
}
输出:
val2 is greater than val1

示例2:

// C# program to demonstrate 
// Byte.CompareTo(byte) 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // checking the condition 
        // calling check() method 
        check((byte)10, (byte)20); 
        check((byte)30, (byte)20); 
        check((byte)10, (byte)10); 
        check((byte)5, (byte)7); 
        check((byte)40, (byte)50); 
        check((byte)1, (byte)2); 
    } 
  
    // Defining the check method 
    public static void check(byte v1, byte v2) 
    { 
  
        // getting compared constant 
        // using CompareTo() method 
        int i = v1.CompareTo(v2); 
  
        // checking the condition 
        if (i > 0) 
            Console.WriteLine(v1 + " is greater than " + v2); 
  
        else if (i < 0) 
            Console.WriteLine(v1 + " is less than " + v2); 
  
        else
            Console.WriteLine(v1 + " is equal to " + v2); 
    } 
}
输出:
10 is less than 20
30 is greater than 20
10 is equal to 10
5 is less than 7
40 is less than 50
1 is less than 2

参考:



相关用法


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