Export (0) Print
Expand All

Single.CompareTo Method

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

[Visual Basic]
Public Overridable Function CompareTo( _
   ByVal value As Object _
) As Integer
[C#]
public virtual int CompareTo(
 object value
);
[C++]
public: virtual int CompareTo(
 Object* value
);
[JScript]
public function CompareTo(
   value : Object
) : int;

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).

Exceptions

Exception Type Condition
ArgumentException value is not a Single.

Remarks

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) 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.

Example

[Visual Basic, C#, C++] The following sample demonstrates the CompareTo method.

[Visual Basic] 
Obj1 = CType(450, Single)

If A.CompareTo(Obj1) < 0 Then
    Console.WriteLine(A.ToString() + " is less than " + Obj1.ToString() + ".")
End If

If (A.CompareTo(Obj1) > 0) Then
    Console.WriteLine(A.ToString() + " is greater than " + Obj1.ToString() + ".")
End If

If (A.CompareTo(Obj1) = 0) Then
    Console.WriteLine(A.ToString() + " equals " + Obj1.ToString() + ".")
End If

[C#] 
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());
}

[C++] 
obj1 = __box((Single)450);

if (__box(a)->CompareTo(obj1) < 0) {
   Console::WriteLine(S" {0} is less than {1}.", __box(a), obj1);
}

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

if (__box(a)->CompareTo(obj1) == 0) {
   Console::WriteLine(S" {0} equals {1}.", __box(a), obj1);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Single Structure | Single Members | System Namespace | Equals

Show:
© 2014 Microsoft