ValueTuple
重要事项:
- 它实现了IStructuralComparable , IStructuralEquatable , IComparable , IComparable
> , IEquatable> 和ITuple接口。 - 它在系统名称空间下定义。
- 它还可以存储重复的元素。
- 字段是可变的。因此,您可以更改ValueTuple
的值。 - 在这里,像Item1和Item2这样的成员不是属性。
- 它是值类型而不是引用类型。
- 它表示将多个数据合并为一个数据集。
- 它允许在单个参数的帮助下将多个值传递给方法。
建设者
Constructor | Description |
---|---|
ValueTuple |
Initializes a new ValueTuple |
场地
Field | Description | ||
---|---|---|---|
Item1 | Gets the value of the current ValueTuple |
Item2 | Gets the value of the current ValueTuple |
例子:
// C# program to illustrate how to
// access the element of ValueTuple
using System;
class GFG {
// Main Method
static public void Main()
{
// Creating a value tuple
// Using Create method
var Mylibrary = ValueTuple.Create(3456, "The Guide");
// Display the element of the given value tuple
Console.WriteLine("Book Details: ");
Console.WriteLine("Book Id: {0}", Mylibrary.Item1);
Console.WriteLine("Book Name: {0}", Mylibrary.Item2);
}
}
输出:
Book Details:
Book Id: 3456
Book Name: The Guide
方法
Method | Description |
---|---|
CompareTo(ValueTuple) | Compares the current ValueTuple |
Equals(Object) | Returns a value that indicates whether the current ValueTuple |
Equals(ValueTuple) | Returns a value that indicates whether the current ValueTuple |
GetHashCode() | Calculates the hash code for the current ValueTuple |
ToString() | Returns a string that represents the value of this ValueTuple |
例子:
// C# program to Check the given
// value tuples are equal or not
using System;
class GFG {
// Main method
static public void Main()
{
// Creating 2-ValueTuple
// Using Create method
var T1 = ValueTuple.Create(346, 784);
var T2 = ValueTuple.Create(346, 7743);
// Check if both the value tuples
// are equal or not
if (T1.Equals(T2))
{
Console.WriteLine("Code is correct...!!");
}
else
{
Console.WriteLine("Incorrect Code...!!");
}
}
}
输出:
Incorrect Code...!!
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.valuetuple-2?view=netframework-4.8