Partager via


Single.CompareTo Method (Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Compares this instance to a specified object and returns an integer that indicates whether the value of this instance is greater than, less than, or equal to the value of the specified object.

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

Syntax

'Declaration
Public Function CompareTo ( _
    value As Object _
) As Integer
public int CompareTo(
    Object value
)

Parameters

  • value
    Type: System.Object
    An object to compare, or nulla null reference (Nothing in Visual Basic).

Return Value

Type: System.Int32
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 nulla null reference (Nothing in Visual Basic).

Implements

IComparable.CompareTo(Object)

Exceptions

Exception Condition
ArgumentException

value is not a Single.

Remarks

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

The value parameter must be nulla 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.

Examples

The following code example demonstrates the CompareTo method.

Obj1 = CType(450, Single)

If A.CompareTo(Obj1) < 0 Then
   outputBlock.Text &= A.ToString() + " is less than " + Obj1.ToString() + "." & vbCrLf
End If

If (A.CompareTo(Obj1) > 0) Then
   outputBlock.Text &= A.ToString() + " is greater than " + Obj1.ToString() + "." & vbCrLf
End If

If (A.CompareTo(Obj1) = 0) Then
   outputBlock.Text &= A.ToString() + " equals " + Obj1.ToString() + "." & vbCrLf
End If
obj1 = (Single)450;

if (a.CompareTo(obj1) < 0)
{
   outputBlock.Text += String.Format("{0} is less than {1}.", a.ToString(), obj1.ToString()) + "\n";
}

if (a.CompareTo(obj1) > 0)
{
   outputBlock.Text += String.Format("{0} is greater than {1}.", a.ToString(), obj1.ToString()) + "\n";
}

if (a.CompareTo(obj1) == 0)
{
   outputBlock.Text += String.Format("{0} equals {1}.", a.ToString(), obj1.ToString()) + "\n";
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.