C# |布尔型。比较(对象)方法
原文:https://www . geeksforgeeks . org/c-sharp-boolean-compare tobject-method/
布尔。比较(对象)方法用于将当前实例与指定的对象进行比较,并返回一个整数,该整数显示了它们之间的关系。
语法:
public int CompareTo (object obj);
这里,需要一个对象来与当前实例或 null 进行比较。
返回值:该方法返回一个有符号整数,表示当前实例和对象的相对顺序。
- 小于零:如果这个实例是假而 obj 是真。
- 零:如果这个实例和 obj 相等(要么两者都是真要么两者都是假)。
- 大于零:如果此实例为真而对象为假或空。
异常:如果对象不是布尔值,它将抛出参数异常。
例 1:
// C# program to demonstrate the
// Boolean.CompareTo(Object) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring and initializing value1
bool value1 = true;
// Declaring and initializing value2
object value2 = true;
// 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 Boolean");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
Output:
True is equal to True
示例 2: 适用于参数异常
// C# program to demonstrate the
// Boolean.CompareTo(Object) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring and initializing value1
bool value1 = true;
// Declaring and initializing value2
object value2 = 53554;
// 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 Boolean");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
Output:
value2 must be Boolean
Exception Thrown: System.ArgumentException
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处