Single.CompareTo() Method in C# with Examples
Single.CompareTo() Method is used to compare the current instance to a specified object or to another Single instance and returns an integer which shows whether the value of the current instance is greater than, equal to, or less than the value of the specified object or the other Single instance. There are 2 methods in the overload list of this method as follows:
- CompareTo(Single) Method
- CompareTo(Object) Method
Single.CompareTo(Single) Method
This method is used to compare the current instance to a specified single-precision floating-point number and returns an integer which indicates whether the value of this instance is less than, equal to, or greater than the value of the specified single-precision floating-point number.
Syntax:
public int CompareTo (float value);
Here, it takes a single-precision floating-point number to compare.
Return Value: It returns a 32-bit signed number indicating the relative values of current instance and value parameter as follows:
- Less than Zero: if Current Instance < value or Current instance is not a number (NaN) and value is a number.
- Zero: if Current Instance = value or Both the current instance and value are not a number (NaN), PositiveInfinity, or NegativeInfinity.
- Greater than Zero: if Current Instance > value or the current instance is a number and value is not a number (NaN).
Below programs illustrate the use of Single.CompareTo(Single) Method:
Example 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
Example 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
This method is used to compare the current instance to a specified object and returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object.
Syntax:
public int CompareTo (object value);
Here, it takes the object to compare with this instance, or null.
Return Value: It returns a 32-bit signed number indicating the relative values of current instance and value parameter as follows:
- Less than Zero: if Current Instance < value or Current instance is not a number (NaN) and value is a number.
- Zero: if Current Instance = value or Both the current instance and value are not a number (NaN), PositiveInfinity, or NegativeInfinity.
- Greater than Zero: if Current Instance > value or the current instance is a number and value is not a number (NaN).
Exception: It throws ArgumentException if value is not an Single.
Below programs illustrate the use of Single.CompareTo(Object) Method
Example 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
Example 2: For 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
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.single.compareto?view=netstandard-2.1
Contact Us