C# | Tuple 类T3】

原文:https://www.geeksforgeeks.org/c-sharp-tuple-1-class/

元组类用于创建 1 元组或单元组,其中只包含一个元素。您可以通过调用 元组< T1 >构造函数 或静态 元组来实例化元组对象。创建 方法。您可以通过使用只读项目 1 实例属性来检索元组的单个元素的值。

要点:

  • 它实现了结构可比较结构可比较我可比较接口。
  • 它是在系统命名空间下定义的。
  • 它将多个数据表示成一个数据集。
  • 它允许我们创建、操作和访问数据集。
  • 它从一个方法返回多个值,而不使用 out 参数。
  • 它允许在单个参数的帮助下向一个方法传递多个值。
  • 它还可以存储重复的元素。

构造器

| 构造器 | 描述 | | 元组(T1)T3】 | 初始化元组类的新实例。 |

财产

| 财产 | 描述 | | 第 1 项 | 获取元组对象的单个元素的值。 |

示例:

// C# program to illustrate the constructor 
// and property of class Tuple<T1>
using System;

class GFG {

    // Main method
    static public void Main()
    {

        // Creating 1-Tuple
        // Using Tuple<T1>(T1)
        Tuple<int> mytuple = new Tuple<int>(22);

        // Accessing the values
        Console.WriteLine("Value of the Element is: " + mytuple.Item1);
    }
}

Output:

Value of the Element is: 22

方法

| 方法 | 描述 | | 等于(对象) | 返回一个值,该值指示当前元组对象是否等于指定对象。 | | GethashCode() | 返回当前元组对象的哈希代码。 | | 【gettype() | 获取当前实例的类型。 | | MemberWiseCrone() | 创建当前对象的浅拷贝。 | | ToString() | 返回表示该元组实例的值的字符串。 |

示例:

// 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>(T1)
        Tuple<int> mytuple1 = new Tuple<int>(22);
        Tuple<int> mytuple2 = new Tuple<int>(22);

        // Using Equals method
        if (mytuple1.Equals(mytuple2))
        {
            Console.WriteLine("Tuple Matched..");
        }

        else
        {
            Console.WriteLine("Tuple not matched..");
        }
    }
}

Output:

Tuple Matched..

参考: