Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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

Value Condition
-1 The value of this instance is less than the value of value.
0 The value of this instance is equal to the value of value.
1 The value of this instance is greater than the value of value.

-or-

value is a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
ArgumentException value is not a TimeSpan.

Remarks

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

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

Example

[Visual Basic, C#, C++] The following code example compares several TimeSpan and other objects to a reference TimeSpan using the CompareTo method.

[Visual Basic] 
' Example of the TimeSpan.CompareTo( Object ) and 
' TimeSpan.Equals( Object ) methods.
Imports System
Imports Microsoft.VisualBasic

Module TSCompToEqualsObjDemo
    
    ' Compare the TimeSpan to the Object parameters, 
    ' and display the Object parameters with the results.
    Sub CompTimeSpanToObject( Left as TimeSpan, Right as Object, _
        RightText as String )

        Console.WriteLine( "{0,-33}{1}", "Object: " & RightText, _
            Right )
        Console.WriteLine( "{0,-33}{1}", "Left.Equals( Object )", _
            Left.Equals( Right ) )
        Console.Write( "{0,-33}", "Left.CompareTo( Object )" )

        ' Catch the exception if CompareTo( ) throws one.
        Try
            Console.WriteLine( "{0}" & vbCrLf, _
                Left.CompareTo( Right ) )
        Catch ex As Exception
            Console.WriteLine( "Error: {0}" & vbCrLf, ex.Message )
        End Try
    End Sub

    Sub Main( )
        Dim Left as new TimeSpan( 0, 5, 0 )

        Console.WriteLine( _
            "This example of the TimeSpan.Equals( Object ) " & _
            "and " & vbCrLf & "TimeSpan.CompareTo( Object ) " & _
            "methods generates the " & vbCrLf & _
            "following output by creating several " & _
            "different TimeSpan " & vbCrLf & "objects and " & _
            "comparing them with a 5-minute TimeSpan." & vbCrLf )
        Console.WriteLine( "{0,-33}{1}" & vbCrLf, _
            "Left: TimeSpan( 0, 5, 0 )", Left )

        ' Create objects to compare with a 5-minute TimeSpan.
        CompTimeSpanToObject( Left, new TimeSpan( 0, 0, 300 ), _
            "TimeSpan( 0, 0, 300 )" )
        CompTimeSpanToObject( Left, new TimeSpan( 0, 5, 1 ), _
            "TimeSpan( 0, 5, 1 )" )
        CompTimeSpanToObject( Left, new TimeSpan( 0, 5, -1 ), _
            "TimeSpan( 0, 5, -1 )" )
        CompTimeSpanToObject( Left, new TimeSpan( 3000000000 ), _
            "TimeSpan( 3000000000 )" )
        CompTimeSpanToObject( Left, 3000000000L, "Long 3000000000L" )
        CompTimeSpanToObject( Left, "00:05:00", _
            "String ""00:05:00""" )
    End Sub
End Module 

' This example of the TimeSpan.Equals( Object ) and
' TimeSpan.CompareTo( Object ) methods generates the
' following output by creating several different TimeSpan
' objects and comparing them with a 5-minute TimeSpan.
' 
' Left: TimeSpan( 0, 5, 0 )        00:05:00
' 
' Object: TimeSpan( 0, 0, 300 )    00:05:00
' Left.Equals( Object )            True
' Left.CompareTo( Object )         0
' 
' Object: TimeSpan( 0, 5, 1 )      00:05:01
' Left.Equals( Object )            False
' Left.CompareTo( Object )         -1
' 
' Object: TimeSpan( 0, 5, -1 )     00:04:59
' Left.Equals( Object )            False
' Left.CompareTo( Object )         1
' 
' Object: TimeSpan( 3000000000 )   00:05:00
' Left.Equals( Object )            True
' Left.CompareTo( Object )         0
' 
' Object: Long 3000000000L         3000000000
' Left.Equals( Object )            False
' Left.CompareTo( Object )         Error: Object must be of type TimeSpan.
' 
' Object: String "00:05:00"        00:05:00
' Left.Equals( Object )            False
' Left.CompareTo( Object )         Error: Object must be of type TimeSpan.

[C#] 
// Example of the TimeSpan.CompareTo( Object ) and 
// TimeSpan.Equals( Object ) methods.
using System;

class TSCompToEqualsObjDemo
{
    // Compare the TimeSpan to the Object parameters, 
    // and display the Object parameters with the results.
    static void CompTimeSpanToObject( TimeSpan Left, object Right, 
        string RightText )
    {
        Console.WriteLine( "{0,-33}{1}", "Object: " + RightText, 
            Right );
        Console.WriteLine( "{0,-33}{1}", "Left.Equals( Object )", 
            Left.Equals( Right ) );
        Console.Write( "{0,-33}", "Left.CompareTo( Object )" );

        // Catch the exception if CompareTo( ) throws one.
        try
        {
            Console.WriteLine( "{0}\n", Left.CompareTo( Right ) );
        }
        catch( Exception ex )
        {
            Console.WriteLine( "Error: {0}\n", ex.Message );
        }
    }

    static void Main( )
    {
        TimeSpan Left = new TimeSpan( 0, 5, 0 );

        Console.WriteLine(
            "This example of the TimeSpan.Equals( Object ) " +
            "and \nTimeSpan.CompareTo( Object ) methods generates " +
            "the \nfollowing output by creating several different " +
            "TimeSpan \nobjects and comparing them with a " +
            "5-minute TimeSpan.\n" );
        Console.WriteLine( "{0,-33}{1}\n", 
            "Left: TimeSpan( 0, 5, 0 )", Left );

        // Create objects to compare with a 5-minute TimeSpan.
        CompTimeSpanToObject( Left, new TimeSpan( 0, 0, 300 ), 
            "TimeSpan( 0, 0, 300 )" );
        CompTimeSpanToObject( Left, new TimeSpan( 0, 5, 1 ), 
            "TimeSpan( 0, 5, 1 )" );
        CompTimeSpanToObject( Left, new TimeSpan( 0, 5, -1 ), 
            "TimeSpan( 0, 5, -1 )" );
        CompTimeSpanToObject( Left, new TimeSpan( 3000000000 ), 
            "TimeSpan( 3000000000 )" );
        CompTimeSpanToObject( Left, 3000000000L, 
            "long 3000000000L" );
        CompTimeSpanToObject( Left, "00:05:00", 
            "string \"00:05:00\"" );
    } 
} 

/*
This example of the TimeSpan.Equals( Object ) and
TimeSpan.CompareTo( Object ) methods generates the
following output by creating several different TimeSpan
objects and comparing them with a 5-minute TimeSpan.

Left: TimeSpan( 0, 5, 0 )        00:05:00

Object: TimeSpan( 0, 0, 300 )    00:05:00
Left.Equals( Object )            True
Left.CompareTo( Object )         0

Object: TimeSpan( 0, 5, 1 )      00:05:01
Left.Equals( Object )            False
Left.CompareTo( Object )         -1

Object: TimeSpan( 0, 5, -1 )     00:04:59
Left.Equals( Object )            False
Left.CompareTo( Object )         1

Object: TimeSpan( 3000000000 )   00:05:00
Left.Equals( Object )            True
Left.CompareTo( Object )         0

Object: long 3000000000L         3000000000
Left.Equals( Object )            False
Left.CompareTo( Object )         Error: Object must be of type TimeSpan.

Object: string "00:05:00"        00:05:00
Left.Equals( Object )            False
Left.CompareTo( Object )         Error: Object must be of type TimeSpan.
*/

[C++] 
// Example of the TimeSpan::CompareTo( Object* ) and 
// TimeSpan::Equals( Object* ) methods.
#using <mscorlib.dll>
using namespace System;

// Compare the TimeSpan to the Object parameters, 
// and display the Object parameters with the results.
void CompTimeSpanToObject( TimeSpan Left, Object* Right, 
    String* RightText )
{
    Console::WriteLine( S"{0,-33}{1}", 
        String::Concat( S"Object: ", RightText ), Right );
    Console::WriteLine( S"{0,-33}{1}", S"Left.Equals( Object )", 
        __box( Left.Equals( Right ) ) );
    Console::Write( S"{0,-33}", S"Left.CompareTo( Object )" );

    // Catch the exception if CompareTo( ) throws one.
    try
    {
        Console::WriteLine( S"{0}\n", 
            __box( Left.CompareTo( Right ) ) );
    }
    catch( Exception* ex )
    {
        Console::WriteLine( S"Error: {0}\n", ex->Message );
    }
}

void main( )
{
    TimeSpan Left = TimeSpan( 0, 5, 0 );

    Console::WriteLine(
        S"This example of the TimeSpan::Equals( Object* ) " 
        S"and \nTimeSpan::CompareTo( Object* ) methods generates " 
        S"the \nfollowing output by creating several different " 
        S"TimeSpan \nobjects and comparing them with a " 
        S"5-minute TimeSpan.\n" );
    Console::WriteLine( S"{0,-33}{1}\n", 
        S"Left: TimeSpan( 0, 5, 0 )", __box( Left ) );

    // Create objects to compare with a 5-minute TimeSpan.
    CompTimeSpanToObject( Left, __box( TimeSpan( 0, 0, 300 ) ), 
        S"TimeSpan( 0, 0, 300 )" );
    CompTimeSpanToObject( Left, __box( TimeSpan( 0, 5, 1 ) ), 
        S"TimeSpan( 0, 5, 1 )" );
    CompTimeSpanToObject( Left, __box( TimeSpan( 0, 5, -1 ) ), 
        S"TimeSpan( 0, 5, -1 )" );
    CompTimeSpanToObject( Left, __box( TimeSpan( 3000000000 ) ), 
        S"TimeSpan( 3000000000 )" );
    CompTimeSpanToObject( Left, __box( 3000000000L ), 
        S"__int64 3000000000L" );
    CompTimeSpanToObject( Left, S"00:05:00", S"String \"00:05:00\"" );
} 

/*
This example of the TimeSpan::Equals( Object* ) and
TimeSpan::CompareTo( Object* ) methods generates the
following output by creating several different TimeSpan
objects and comparing them with a 5-minute TimeSpan.

Left: TimeSpan( 0, 5, 0 )        00:05:00

Object: TimeSpan( 0, 0, 300 )    00:05:00
Left.Equals( Object )            True
Left.CompareTo( Object )         0

Object: TimeSpan( 0, 5, 1 )      00:05:01
Left.Equals( Object )            False
Left.CompareTo( Object )         -1

Object: TimeSpan( 0, 5, -1 )     00:04:59
Left.Equals( Object )            False
Left.CompareTo( Object )         1

Object: TimeSpan( 3000000000 )   00:05:00
Left.Equals( Object )            True
Left.CompareTo( Object )         0

Object: __int64 3000000000L      3000000000
Left.Equals( Object )            False
Left.CompareTo( Object )         Error: Object must be of type TimeSpan.

Object: String "00:05:00"        00:05:00
Left.Equals( Object )            False
Left.CompareTo( Object )         Error: Object must be of type TimeSpan.
*/

[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

TimeSpan Structure | TimeSpan Members | System Namespace | Object | Compare | Equals

Show:
© 2015 Microsoft