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

📅  最后修改于: 2021-05-29 20:39:02             🧑  作者: Mango

ValueTuple 结构用于创建n-ValueTuple,其中n = 8或大于8。它表示包含八个或八个以上的值元组。元素。它提供值元组的运行时实现。您可以使用ValueTuple (T1,T2, T3,T4,T5,T6,T7,TRest)构造函数,或者使用ValueTuple.Create方法或仅通过使用parenthesis() 。您可以使用默认属性来检索值元组的未命名元素的值,或者可以借助其名称直接访问命名元素。

重要事项:

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

建设者

Constructor Description
ValueTuple(T1, T2, T3, T4, T5, T6, T7, TRest) 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. TRest Any generic value tuple instance that defines the types of the tuple’s remaining elements.

例子:

// 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", 
                      ValueTuple.Create("Swami and Friends", "The Dark Room", 
                                       "Mr. Sampath", "Grandmother's Tale"));
  
        // 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);
        Console.WriteLine("Other Novels: {0}", Mylibrary.Rest);
    }
}

输出:

Book Details: 
Book Id: 3456
Book Name: The Guide
Author Name: R. K. Narayan
Publication date: 1958
Gener: Philosophical novel
Language: English
Country: India
Other Novels: ((Swami and Friends, The Dark Room, Mr. Sampath, Grandmother's Tale))

方法

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 8-ValueTuple
        // Using Create method
        var T1 = ValueTuple.Create(3, 2, 4, 5, 6, 7, 6, 10);
        var T2 = ValueTuple.Create(3, 2, 4, 5, 6, 7, 6, 1);
  
        // 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-8?view=netframework-4.8