Export (0) Print
Expand All

Single.CompareTo Method (Object)

Compares this instance to a specified object and returns an indication of their relative values.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public int CompareTo (
	Object value
)
public final int CompareTo (
	Object value
)
public final function CompareTo (
	value : Object
) : int
Not applicable.

Parameters

value

An object to compare, or a null reference (Nothing in Visual Basic).

Return Value

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 a null reference (Nothing in Visual Basic).

Exception typeCondition

ArgumentException

value is not a Single.

Any instance of Single, regardless of its value, is considered greater than a null reference (Nothing in Visual Basic).

The value parameter must be a null reference (Nothing in Visual Basic) or an instance of Single; otherwise, an exception is thrown.

This method is implemented to support the IComparable interface. Note that, although a NaN is not considered to be equal to another NaN (even itself), the IComparable interface requires that A.CompareTo(A) return zero.

Precision in Comparisons

The precision of floating-point numbers beyond the documented precision is specific to the implementation and version of the .NET Framework. Consequently, a comparison of two particular numbers might change between versions of the .NET Framework because the precision of the numbers' internal representation might change.

The following code example demonstrates the CompareTo method.

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());
}

obj1 = new Float(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());
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft