📜  C#|元组<T1>班级

📅  最后修改于: 2021-05-29 16:38:44             🧑  作者: Mango

Tuple 类用于创建1-tuple或singleton,其中仅包含一个元素。您可以通过调用Tuple 构造函数或通过静态Tuple.Create方法来实例化Tuple 对象。您可以使用只读的Item1实例属性来检索元组的单个元素的值。

重要事项:

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

建设者

Constructor Description
Tuple(T1) Initializes a new instance of the Tuple class.

财产

Property Description
Item1 Gets the value of the Tuple object’s single element.

例子:

// C# program to illustrate the constructor 
// and property of class Tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 1-Tuple
        // Using Tuple(T1)
        Tuple mytuple = new Tuple(22);
  
        // Accessing the values
        Console.WriteLine("Value of the Element is: " + mytuple.Item1);
    }
}
输出:
Value of the Element is: 22

方法

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 determine the 
// given tuples are equal or not
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 1-Tuple
        // Using Tuple(T1)
        Tuple mytuple1 = new Tuple(22);
        Tuple mytuple2 = new Tuple(22);
  
        // 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-1?view=netframework-4.8