ValueTuple
重要事项:
- 它实现了IStructuralComparable , IStructuralEquatable , IComparable,IComparable
>, IEquatable> 和ITuple接口。 - 它在系统名称空间下定义。
- 它还可以存储重复的元素。
- 字段是可变的。因此,您可以更改ValueTuple
Struct的值。 - 在这里,诸如Item1 , Item2 , Item3 , Item4 , Item5 , Item6和Item7之类的成员不是属性。
- 它是值类型而不是引用类型。
- 它表示将多个数据合并为一个数据集。
- 它允许在单个参数的帮助下将多个值传递给方法。
建设者
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 |
Item3 | Gets the value of the current ValueTuple |
Item4 | Gets the value of the current ValueTuple |
Item5 | Gets the value of the current ValueTuple |
Item6 | Gets the value of the current ValueTuple |
Item7 | 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", "R. K. Narayan",
1958, "Philosophical novel", "English", "India");
// 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);
Console.WriteLine("Author Name: {0}", Mylibrary.Item3);
Console.WriteLine("Publication date: {0}", Mylibrary.Item4);
Console.WriteLine("Gener: {0}", Mylibrary.Item5);
Console.WriteLine("Language: {0}", Mylibrary.Item6);
Console.WriteLine("Country: {0}", Mylibrary.Item7);
}
}
输出:
Book Details:
Book Id: 3456
Book Name: The Guide
Author Name: R. K. Narayan
Publication date: 1958
Gener: Philosophical novel
Language: English
Country: India
方法
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 7-ValueTuple
// Using Create method
var T1 = ValueTuple.Create(3, 2, 4, 5, 6, 7, 6);
var T2 = ValueTuple.Create(3, 2, 4, 5, 6, 7, 6);
// Check if both the value
// tuples are equal or not
if (T1.Equals(T2))
{
Console.WriteLine("Code is correct...!!");
}
else
{
Console.WriteLine("Incorrect Code...!!");
}
}
}
输出:
Code is correct...!!
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.valuetuple-7?view=netframework-4.8