C# Single CompareTo(Object)

Description

Single CompareTo(Object) compares this instance to a specified object and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object.

Syntax

Single.CompareTo(Object) has the following syntax.


public int CompareTo(
  Object value
)

Parameters

Single.CompareTo(Object) has the following parameters.

  • value - An object to compare, or null.

Returns

Single.CompareTo(Object) method returns A signed number indicating the relative values of this instance and value. Return Value Description Less than zero This instance is less than value. -or- This instance is not a number (NaN) and value is a number. Zero This instance is equal to value. -or- This instance and value are both not a number (NaN), PositiveInfinity, or NegativeInfinity. Greater than zero This instance is greater than value. -or- This instance is a number and value is not a number (NaN). -or- value is null.

Example

The following code example demonstrates the CompareTo method.


using System;/*from  w  w w  .  j av  a2 s .  c  o  m*/

public class Example
{
    public static void Main()
    {
        float a = 16.5457f;
        float operand = 3.8899982f;
        object value2 = a * operand / operand;
        Console.WriteLine("Comparing {0} and {1}: {2}\n",
                          a, value2, a.CompareTo(value2));
        Console.WriteLine("Comparing {0:R} and {1:R}: {2}",
                          a, value2, a.CompareTo(value2));

        Object obj1 = (Single)450;

        if (a.CompareTo(obj1) < 0)
        {
            Console.WriteLine("{0} is less than {1}.", a.ToString(), obj1.ToString());
        }

        if (a.CompareTo(obj1) > 0)
        {
            Console.WriteLine("{0} is greater than {1}.", a.ToString(), obj1.ToString());
        }

        if (a.CompareTo(obj1) == 0)
        {
            Console.WriteLine("{0} equals {1}.", a.ToString(), obj1.ToString());
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version