📜  C#|价值元组<T1,T2,T3,T4>结构

📅  最后修改于: 2021-05-30 00:55:06             🧑  作者: Mango

ValueTuple 结构用于创建四元组或4-ValueTuple。它代表一个存储四个元素的值元组。它提供值元组的运行时实现。您可以使用ValueTuple (T1,T2,T3,T4)构造函数或使用ValueTuple.Create方法或仅通过以下方式创建ValueTuple 结构的实例使用括号() 。您可以使用默认属性来检索ValueTuple的未命名元素的值,或者可以借助其名称直接访问命名元素。

重要事项:

  • 它实现了IStructuralComparableIStructuralEquatableIComparableIComparable >IEquatable >ITuple接口。
  • 它在系统名称空间下定义。
  • 它还可以存储重复的元素。
  • 字段是可变的。因此,您可以更改ValueTuple 的值。
  • 在这里,诸如Item1Item2Item3Item4之类的成员不是属性。
  • 它是值类型而不是引用类型。
  • 它表示将多个数据合并为一个数据集。
  • 它允许在单个参数的帮助下将多个值传递给方法。

建设者

Constructor Description
ValueTuple(T1, T2, T3, T4) Initializes a new ValueTuple instance.

场地

Field Description
Item1 Gets the value of the current ValueTuple instance’s first element. Item2 Gets the value of the current ValueTuple instance’s second element. Item3 Gets the value of the current ValueTuple instance’s third element. Item4 Gets the value of the current ValueTuple instance’s fourth element.

例子:

// 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);
  
        // 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);
    }
}
输出:
Book Details: 
Book Id: 3456
Book Name: The Guide
Author Name: R. K. Narayan
Publication date: 1958

方法

Method Description
CompareTo(ValueTuple) Compares the current ValueTuple instance to a specified ValueTuple instance.
Equals(Object) Returns a value that indicates whether the current ValueTuple instance is equal to a specified object.
Equals(ValueTuple) Returns a value that indicates whether the current ValueTuple instance is equal to a specified ValueTuple instance.
GetHashCode() Calculates the hash code for the current ValueTuple instance.
ToString() Returns a string that represents the value of this ValueTuple instance.

例子:

// C# program to check the given value
// tuples are equal or not
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 4-ValueTuple
        // Using Create method
        var T1 = ValueTuple.Create(346, 784, 45, 22);
        var T2 = ValueTuple.Create(346, 7743, 56, 22);
  
        // 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-4?view=netframework-4.8