ValueTuple是C#7.0中引入的结构,表示值类型Tuple。它已包含在.NET Framework 4.7或更高版本中。它允许您存储一个数据集,该数据集包含可能彼此相关或不相关的多个值。
您还可以使用CompareTo(ValueTuple
句法:
public int CompareTo (ValueTuple other);
返回类型:此方法的返回类型为System.Int32 。并且它总是返回一个带符号的整数,该整数指示该实例和其他实例的相对位置,并按照下面的列表所示的排序顺序:
- 实例的负整数在另一个之前。
- 实例的零和其他实例在排序顺序中的位置相同。
- 实例的正整数紧随另一个。
范例1:
// C# program to illustrate the
// use of CompareTo method
using System;
namespace exampleofvaluetuple {
class GFG {
// Main Method
static void Main(string[] args)
{
// 8-ValueTuple
var t1 = ValueTuple.Create(43, 34, 22, 7, 8,
29, 30, ValueTuple.Create(1, 2, 3));
var t2 = ValueTuple.Create(78, 98, 76, 67, 54,
78, 89, ValueTuple.Create(10, 22, 30));
Console.WriteLine("Result 1: {0}", t1.CompareTo(t2));
// 8-ValueTuple
var p1 = ValueTuple.Create(86, 99, 67, 88, 89,
96, 78, ValueTuple.Create(90, 80, 70));
var p2 = ValueTuple.Create(44, 22, 06, 55, 12,
16, 23, ValueTuple.Create(9, 8, 7));
Console.WriteLine("Result 2: {0}", p1.CompareTo(p2));
// 8-ValueTuple
var q1 = ValueTuple.Create(10, 20, 30, 40, 50,
60, 70, ValueTuple.Create(22, 33, 44));
var q2 = ValueTuple.Create(10, 20, 30, 40, 50,
60, 70, ValueTuple.Create(22, 33, 44));
Console.WriteLine("Result 3: {0}", q1.CompareTo(q2));
}
}
}
输出:
Result 1: -1
Result 2: 1
Result 3: 0
范例2:
// C# program to illustrate the
// use of CompareTo method
using System;
namespace exampleofvaluetuple {
class GFG {
// Main Method
static void Main(string[] args)
{
// 8-ValueTuple
var t1 = ValueTuple.Create(44, 55, 66, 77, 88,
99, 33, ValueTuple.Create(1, 2, 3));
var t2 = ValueTuple.Create(44, 55, 66, 77, 88,
99, 33, ValueTuple.Create(1, 2, 3));
// Using CompareTo Method
if (t1.CompareTo(t2) == 0) {
Console.WriteLine("Value tuples are equal!!");
}
else {
Console.WriteLine("Value tuples are not equal!!");
}
}
}
}
输出:
Value tuples are equal!!