Partager via


TimeSpan.Equals Method (Object)

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

Returns a value indicating whether this instance is equal to a specified object.

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

Syntax

'Declaration
Public Overrides Function Equals ( _
    value As Object _
) As Boolean
public override bool Equals(
    Object value
)

Parameters

  • value
    Type: System.Object
    An object to compare with this instance.

Return Value

Type: System.Boolean
true if value is a TimeSpan object that represents the same time interval as the current TimeSpan structure; otherwise, false.

Examples

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

' Example of the TimeSpan.CompareTo( Object ) and 
' TimeSpan.Equals( Object ) methods.

Module Example

   ' Compare the TimeSpan to the Object parameters, 
   ' and display the Object parameters with the results.
   Sub CompTimeSpanToObject(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Left As TimeSpan, ByVal Right As Object, _
       ByVal RightText As String)

      outputBlock.Text &= String.Format("{0,-33}{1}", "Object: " & RightText, _
          Right) & vbCrLf
      outputBlock.Text &= String.Format("{0,-33}{1}", "Left.Equals( Object ) & vbCrLf", _
          Left.Equals(Right))
      outputBlock.Text &= String.Format("{0,-33}", "Left.CompareTo( Object )")

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

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim Left As New TimeSpan(0, 5, 0)

      outputBlock.Text &= String.Format( _
          "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) & vbCrLf
      outputBlock.Text &= String.Format("{0,-33}{1}" & vbCrLf, _
          "Left: TimeSpan( 0, 5, 0 )", Left) & vbCrLf

      ' Create objects to compare with a 5-minute TimeSpan.
      CompTimeSpanToObject(outputBlock, Left, New TimeSpan(0, 0, 300), _
          "TimeSpan( 0, 0, 300 )")
      CompTimeSpanToObject(outputBlock, Left, New TimeSpan(0, 5, 1), _
          "TimeSpan( 0, 5, 1 )")
      CompTimeSpanToObject(outputBlock, Left, New TimeSpan(0, 5, -1), _
          "TimeSpan( 0, 5, -1 )")
      CompTimeSpanToObject(outputBlock, Left, New TimeSpan(3000000000), _
          "TimeSpan( 3000000000 )")
      CompTimeSpanToObject(outputBlock, Left, 3000000000L, "Long 3000000000L")
      CompTimeSpanToObject(outputBlock, 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.
// Example of the TimeSpan.CompareTo( Object ) and 
// TimeSpan.Equals( Object ) methods.
using System;

class Example
{
   // Compare the TimeSpan to the Object parameters, 
   // and display the Object parameters with the results.
   static void CompTimeSpanToObject(System.Windows.Controls.TextBlock outputBlock, TimeSpan Left, object Right,
       string RightText)
   {
      outputBlock.Text += String.Format("{0,-33}{1}", "Object: " + RightText,
          Right) + "\n";
      outputBlock.Text += String.Format("{0,-33}{1}", "Left.Equals( Object )",
          Left.Equals(Right)) + "\n";
      outputBlock.Text += String.Format("{0,-33}", "Left.CompareTo( Object )");

      // Catch the exception if CompareTo( ) throws one.
      try
      {
         outputBlock.Text += String.Format("{0}\n", Left.CompareTo(Right)) + "\n";
      }
      catch (Exception ex)
      {
         outputBlock.Text += String.Format("Error: {0}\n", ex.Message) + "\n";
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      TimeSpan Left = new TimeSpan(0, 5, 0);

      outputBlock.Text += String.Format(
          "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") + "\n";
      outputBlock.Text += String.Format("{0,-33}{1}\n",
          "Left: TimeSpan( 0, 5, 0 )", Left) + "\n";

      // Create objects to compare with a 5-minute TimeSpan.
      CompTimeSpanToObject(outputBlock, Left, new TimeSpan(0, 0, 300),
          "TimeSpan( 0, 0, 300 )");
      CompTimeSpanToObject(outputBlock, Left, new TimeSpan(0, 5, 1),
          "TimeSpan( 0, 5, 1 )");
      CompTimeSpanToObject(outputBlock, Left, new TimeSpan(0, 5, -1),
          "TimeSpan( 0, 5, -1 )");
      CompTimeSpanToObject(outputBlock, Left, new TimeSpan(3000000000),
          "TimeSpan( 3000000000 )");
      CompTimeSpanToObject(outputBlock, Left, 3000000000L,
          "long 3000000000L");
      CompTimeSpanToObject(outputBlock, 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.
*/

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.