C#| TupleT1,T2,T3,T4类

2021年5月7日13:54:56 发表评论 595 次浏览

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

重要事项:

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

建设者

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

属性

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

例子:

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

输出如下:

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

方法

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

例子:

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

输出如下:

Tuple Matched..

参考:

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

木子山

发表评论

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