📌  相关文章
📜  C#|元组<T1,T2,T3,T4,T5,T6,T7,TRest>班级

📅  最后修改于: 2021-05-29 23:28:56             🧑  作者: Mango

Tuple 类用于创建n = 8或大于8的n元组。它表示包含八个或八个以上元素的元组在里面。您可以通过调用Tuple (T1,T2, T3,T4,T5,T6,T7,TRest)构造函数或通过静态Tuple.Create方法。您可以通过使用只读的Item1Item2Item3Item4Item5Item6Item7Rest实例属性来检索元组元素的值。

重要事项:

  • 它实现了IStructuralComparableIStructuralEquatableIComparable接口。
  • 它在系统名称空间下定义。
  • 您可以在嵌套的帮助下在一个元组中创建八个以上的元素。您可以在Tuple 中的Rest参数的位置处创建一个嵌套的元组。
  • 它表示将多个数据合并为一个数据集。
  • 它使我们能够创建,操作和访问数据集。
  • 它从方法返回多个值,而不使用out参数。
  • 它允许在单个参数的帮助下将多个值传递给方法。
  • 它还可以存储重复的元素。

建设者

Constructor Description
Tuple(T1, T2, T3, T4, T5, T6, T7, TRest) Initializes a new instance of the Tuple class.

财产

Property Description
Item1 Gets the value of the Tuple object’s first element.
Item2 Gets the value of the current Tuple object’s second element.
Item3 Gets the value of the current Tuple object’s third element.
Item4 Gets the value of the current Tuple object’s fourth element.
Item5 Gets the value of the current Tuple object’s fifth element.
Item6 Gets the value of the current Tuple object’s sixth element.
Item7 Gets the value of the current Tuple object’s seventh element.
Rest Gets the current Tuple object’s remaining elements.

例子:

// C# progtam to illustrate the constructor
// and property of Tuple Class
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
        // Creating 8-Tuple
        // Using Tuple(T1, T2, T3, T4, T5, T6, T7, 
        // TRest) constructor
        Tuple> mytuple = new Tuple >(79, 34, 67, "Geeks", 44, 
                                               "GeeksforGeeks", 66, new Tuple(89, 77));
  
       // Accessing the values
        Console.WriteLine("Value of the First Component: " + mytuple.Item1);
        Console.WriteLine("Value of the Second Component: " + mytuple.Item2);
        Console.WriteLine("Value of the Third Component: " + mytuple.Item3);
        Console.WriteLine("Value of the Fourth Component: " + mytuple.Item4);
        Console.WriteLine("Value of the Fifth Component: " + mytuple.Item5);
        Console.WriteLine("Value of the Sixth Component: " + mytuple.Item6);
        Console.WriteLine("Value of the Seventh Component: " + mytuple.Item7);
        Console.WriteLine("Value of the Eighth Component: " + mytuple.Rest);
  
    }
}
输出:
Value of the First Component: 79
Value of the Second Component: 34
Value of the Third Component: 67
Value of the Fourth Component: Geeks
Value of the Fifth Component: 44
Value of the Sixth Component: GeeksforGeeks
Value of the Seventh Component: 66
Value of the Eighth Component: (89, 77)

方法

Method Description
Equals(Object) Returns a value that indicates whether the current Tuple object is equal to a specified object.
GetHashCode() Returns the hash code for the current Tuple object.
GetType() Gets the Type of the current instance.
MemberwiseClone() Creates a shallow copy of the current Object.
ToString() Returns a string that represents the value of this Tuple instance.

例子:

// C# program to check whether the
// given tuples are equal or not
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 8-Tuple
        // Using Tuple(T1, 
        // T2, T3, T4, T5, T6, T7, TRest) constructor
        Tuple > mytuple1 = new Tuple >(20, 40, 90,
                                       89, 33, 66, 87, new Tuple("GeeksforGeeks"));
  
        Tuple > mytuple2 = new Tuple >(20, 40, 90,
                                      89, 33, 66, 87, new Tuple("GeeksforGeeks"));
  
        // Using Equals method
        if (mytuple1.Equals(mytuple2)) 
        {
            Console.WriteLine("Tuple Matched.");
        }
  
        else 
        {
            Console.WriteLine("Tuple Not Matched.");
        }
    }
}
输出:
Tuple Matched.

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.tuple-8?view=netframework-4.8