C# TupleT1,T2,T3类

2021年5月7日13:55:21 发表评论 574 次浏览

Tuple <T1, T2, T3>类用于创建三元组或三元组。它表示一个包含三个元素的元组。你可以通过调用以下对象来实例化Tuple <T1, T2, T3>对象元组<T1, T2, T3>(T1, T2, T3)构造函数或由静态元组创建方法。你可以使用只读检索元组元素的值项目1, 项目2和项目3实例属性。

重要事项:

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

建设者

建设者 描述
元组<T1, T2, T3>(T1, T2, T3) 初始化Tuple <T1, T2, T3>类的新实例。

属性

属性 描述
项目1 获取Tuple <T1, T2, T3>对象的第一个组件的值。
项目2 获取当前Tuple <T1, T2, T3>对象的第二个组件的值。
项目3 获取当前Tuple <T1, T2, T3>对象的第三个组件的值。

例子:

//C# progtam to illustrate the constructor
//and property of Tuple<T1, T2, T3> Class
using System;
  
class GFG {
  
     static public void Main()
     {
         //Creating 3-Tuple
         //Using Tuple<T1, T2, T3>(T1, //T2, T3) constructor
         Tuple<int , int , int> mytuple = new Tuple<int , int , int>(79, 34, 67);
  
         //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);
  
     }
}

输出如下:

Value of the First Component: 79
Value of the Second Component: 34
Value of the Third Component: 67

方法

方法 描述
等于(对象) 返回一个值, 该值指示当前的Tuple <T1, T2, T3>对象是否等于指定的对象。
GetHashCode() 返回当前Tuple <T1, T2, T3>对象的哈希码。
GetType() 获取当前实例的类型。
MemberwiseClone() 创建当前对象的浅表副本。
ToString() 返回表示此Tuple <T1, T2, T3>实例的值的字符串。

例子:

//C# program to check whether the
//given tuples are equal or not
using System;
  
class GFG {
  
     //Main method
     static public void Main()
     {
  
         //Creating 3-Tuple
         //Using Tuple<T1, T2, T3>(T1, T2, T3) constructor
         Tuple<int , int , int> mytuple1 = new Tuple<int , int , int>(20, 40, 90);
         Tuple<int , int , int> mytuple2 = new Tuple<int , int , int>(20, 49, 87);
  
         //Using Equals method
         if (mytuple1.Equals(mytuple2)) 
         {
             Console.WriteLine( "Tuple Matched.." );
         }
  
         else 
         {
             Console.WriteLine( "Tuple not matched.." );
         }
     }
}

输出如下:

Tuple not matched..

参考:

  • https://docs.microsoft.com/en-us/dotnet/api/system.tuple-3?view=netframework-4.8

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: