Export (0) Print
Expand All

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

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

Exception typeCondition

ArgumentException

value is not a TimeSpan.

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 in Visual Basic); otherwise, an exception is thrown.

The following code example compares several TimeSpan structures and other objects to a reference TimeSpan structure using the CompareTo method.

// 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.
*/

// Example of the TimeSpan.CompareTo( Object ) and 
// TimeSpan.Equals( Object ) methods.
import 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 )",
            ((System.Boolean)Convert.ToBoolean
            (left.Equals(right))).toString());
        Console.Write("{0,-33}", "Left.CompareTo( Object )");

        // Catch the exception if CompareTo( ) throws one.
        try {
            Console.WriteLine("{0}\n", String.valueOf(left.CompareTo(right)));
        }
        catch (Exception ex) {
            Console.WriteLine("Error: {0}\n", ex.get_Message());
        }
    } //CompTimeSpanToObject

    public static void main(String[] args)
    {
        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(3000000000L), 
            "TimeSpan( 3000000000 )");
        CompTimeSpanToObject(left, new Long(3000000000L), 
            "long 3000000000L");
        CompTimeSpanToObject(left, "00:05:00","string \"00:05:00\"");
    } //main
} //TSCompToEqualsObjDemo

/*
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: Exception of type 'java.lang.IllegalArgument'
                                 was thrown mentException' was thrown.

Object: string "00:05:00"        00:05:00
left.Equals( Object )            False
left.CompareTo( Object )         Error: Exception of type 'java.lang.IllegalArgument' 
                                 was thrown mentException' was thrown.

Press any key to continue
*/

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft