此方法用于比较两个TimeSpan值,并返回一个整数值,该整数值指示第一个值是小于,等于还是大于第二个值。
用法: public static int Compare (TimeSpan t1, TimeSpan t2);
参数:
11:指定要比较的第一个时间间隔。
t2:指定要比较的第二个时间间隔。
返回值:
-1:如果t1短于t2。
0:如果t1等于t2。
1个:如果t1大于t2。
以下示例程序旨在说明TimeSpan.Compare(TimeSpan,TimeSpan)方法的用法:
示例1:
// C# program to demonstrate the
// TimeSpan.Compare(TimeSpan,
// TimeSpan) Method
using System;
class GFG {
// Main Method
public static void Main()
{
// creating the TimeSpans
TimeSpan t1 = new TimeSpan(3, 22, 35, 33);
TimeSpan t2 = new TimeSpan(1, 11, 15, 16);
if (TimeSpan.Compare(t1, t2) == 1)
Console.Write("t1 is greater than t2");
else if (TimeSpan.Compare(t1, t2) == 0)
Console.Write("t1 is equal to t2");
else
Console.Write("t2 is greater than t1");
}
}
输出:
t1 is greater than t2
示例2:
// C# program to demonstrate the
// TimeSpan.Compare(TimeSpan,
// TimeSpan) Method
using System;
class GFG {
// Main Method
public static void Main()
{
// creating the TimeSpans
TimeSpan t1 = new TimeSpan(3, 22, 35, 33);
TimeSpan t2 = new TimeSpan(4, 31, 15, 10);
if (TimeSpan.Compare(t1, t2) == 1)
Console.Write("t1 is greater than t2");
else if (TimeSpan.Compare(t1, t2) == 0)
Console.Write("t1 is equal to t2");
else
Console.Write("t2 is greater than t1");
}
}
输出:
t2 is greater than t1
示例3:
// C# program to demonstrate the
// TimeSpan.Compare(TimeSpan,
// TimeSpan) Method
using System;
class GFG {
// Main Method
public static void Main()
{
// creating the TimeSpans
TimeSpan t1 = new TimeSpan(3, 22, 35, 33);
TimeSpan t2 = new TimeSpan(3, 22, 35, 33);
if (TimeSpan.Compare(t1, t2) == 1)
Console.Write("t1 is greater than t2");
else if (TimeSpan.Compare(t1, t2) == 0)
Console.Write("t1 is equal to t2");
else
Console.Write("t2 is greater than t1");
}
}
输出:
t1 is equal to t2
参考:
相关用法
- C# Dictionary.Add()用法及代码示例
- C# Math.Abs()方法用法及代码示例
- C# Math.Abs()函数用法及代码示例
- C# Math.Exp()用法及代码示例
- C# Queue.Contains()用法及代码示例
- C# Stack.Pop()用法及代码示例
注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 TimeSpan.Compare() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。