📌  相关文章
📜  C#|价值元组<T1,T2,T3,T4,T5,T6,T7>结构

📅  最后修改于: 2021-05-29 22:42:17             🧑  作者: Mango

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

重要事项:

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

建设者

Constructor Description
ValueTuple(T1, T2, T3, T4, T5, T6, T7) 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. Item5 Gets the value of the current ValueTuple instance’s fifth element. Item6 Gets the value of the current ValueTuple instance’s sixth element. Item7 Gets the value of the current ValueTuple instance’s seventh 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, "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 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 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