SByte.CompareTo()方法用於將當前實例與指定的對象或SByte進行比較,並返回其相對值的指示。此方法的重載列表中有2種方法,如下所示:
- CompareTo(SByte)方法
- CompareTo(Object)方法
SByte.CompareTo(SByte) Method
此方法用於將當前實例與指定的8位帶符號整數進行比較,並返回其相對值的指示。
用法:
public int CompareTo (sbyte value);
在這裏,它需要一個8位有符號整數與當前實例進行比較。
返回值:它返回一個32位帶符號的數字,指示當前實例和value參數的相對值,如下所示:
- 小於零:如果當前實例
- 零:如果當前實例=值
- 大於零:如果當前實例>值
以下示例程序旨在說明SByte.CompareTo(SByte)方法的用法:
示例1:
// C# program to demonstrate the
// SByte.CompareTo(SByte) Method
using System;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing value1
sbyte value1 = 10;
// Declaring and initializing value2
sbyte value2 = 20;
// compare both SByte 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);
}
}
輸出:
10 is less than 20
示例2:
// C# program to demonstrate the
// SByte.CompareTo(SByte) Method
using System;
class GFG {
// Main Method
public static void Main()
{
// calling get() method
get(5, 7);
get(30, 20);
get(10, 20);
get(7, -12);
}
// defining get() method
public static void get(sbyte value1,
sbyte 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 is less than 7 30 is greater than 20 10 is less than 20 7 is greater than -12
SByte.CompareTo(Object) Method
此方法用於將當前實例與指定對象進行比較,並返回其相對值的比較。
用法:
public int CompareTo (object value);
在這裏,它需要對象與該實例進行比較,或者為null。
返回值:它返回一個32位帶符號的數字,指示當前實例和value參數的相對值,如下所示:
- 小於零:如果當前實例
- 零:如果當前實例=值
- 大於零:如果當前實例>值
異常:如果value不是SByte,則拋出ArgumentException。
下麵的程序說明Decimal.CompareTo(Object)方法的用法
示例1:
// C# program to demonstrate the
// SByte.CompareTo(object) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring and initializing value1
sbyte value1 = 10;
// Declaring and initializing value2
// explicit type casting the value 9
object value2 = (sbyte)9;
// compare both sbyte 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 SByte");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
10 is greater than 9
示例2:對於ArgumentException
// C# program to demonstrate the
// SByte.CompareTo(object) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring and initializing value1
sbyte 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 sbyte");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
value2 must be sbyte Exception Thrown: System.ArgumentException
參考:
相關用法
- C# MathF.Tan()用法及代碼示例
- C# MathF.Abs()用法及代碼示例
- C# MathF.Max()用法及代碼示例
- C# MathF.Log()用法及代碼示例
- C# MathF.Min()用法及代碼示例
- C# MathF.Cos()用法及代碼示例
- C# MathF.Exp()用法及代碼示例
- C# MathF.Pow()用法及代碼示例
- C# MathF.Sin()用法及代碼示例
- C# UInt16.GetHashCode用法及代碼示例
- C# Single.IsNegative()用法及代碼示例
- C# Single.IsFinite()用法及代碼示例
- C# Single.CompareTo()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 SByte.CompareTo() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。