VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/single-compareto-method-in-c-sharp-with-examples/

⇱ Single.CompareTo() Method in C# with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Single.CompareTo() Method in C# with Examples

Last Updated : 11 Jul, 2025
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:
Output:
10.5 is less than 20.6
Example 2:
Output:
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:
Output:
10.4 is less than 10.5
Example 2: For ArgumentException
Output:
value2 must be Single
Exception Thrown: System.ArgumentException
Reference:
Comment
Article Tags:

Explore