Tuple<T1,T2,T3,T4,T5> 类

定义

表示 5 元组,或五元组。

generic <typename T1, typename T2, typename T3, typename T4, typename T5>
public ref class Tuple : IComparable, System::Collections::IStructuralComparable, System::Collections::IStructuralEquatable
generic <typename T1, typename T2, typename T3, typename T4, typename T5>
public ref class Tuple : IComparable, System::Collections::IStructuralComparable, System::Collections::IStructuralEquatable, System::Runtime::CompilerServices::ITuple
public class Tuple<T1,T2,T3,T4,T5> : IComparable, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable
public class Tuple<T1,T2,T3,T4,T5> : IComparable, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable, System.Runtime.CompilerServices.ITuple
[System.Serializable]
public class Tuple<T1,T2,T3,T4,T5> : IComparable, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable
type Tuple<'T1, 'T2, 'T3, 'T4, 'T5> = class
    interface IStructuralComparable
    interface IStructuralEquatable
    interface IComparable
type Tuple<'T1, 'T2, 'T3, 'T4, 'T5> = class
    interface IStructuralComparable
    interface IStructuralEquatable
    interface IComparable
    interface ITuple
[<System.Serializable>]
type Tuple<'T1, 'T2, 'T3, 'T4, 'T5> = class
    interface IStructuralEquatable
    interface IStructuralComparable
    interface IComparable
[<System.Serializable>]
type Tuple<'T1, 'T2, 'T3, 'T4, 'T5> = class
    interface IStructuralEquatable
    interface IStructuralComparable
    interface IComparable
    interface ITuple
Public Class Tuple(Of T1, T2, T3, T4, T5)
Implements IComparable, IStructuralComparable, IStructuralEquatable
Public Class Tuple(Of T1, T2, T3, T4, T5)
Implements IComparable, IStructuralComparable, IStructuralEquatable, ITuple

类型参数

T1

此元组的第一个组件的类型。

T2

此元组的第二个组件的类型。

T3

此元组的第三个组件的类型。

T4

此元组的第四个组件的类型。

T5

元组的第五个分量的类型。

继承
Tuple<T1,T2,T3,T4,T5>
属性
实现

注解

元组是具有特定数目和值序列的数据结构。 该 Tuple<T1,T2,T3,T4,T5> 类表示一个 5 元组或五元组,它是具有五个组件的元组。

可以通过调用Tuple<T1,T2,T3,T4,T5>构造函数或静态Tuple.Create<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5)方法来实例Tuple<T1,T2,T3,T4,T5>化对象。 可以使用只读Item1Item2Item3Item4Item5实例属性检索元组组件的值。

元组通常以四种不同的方式使用:

  • 表示单个数据集。 例如,元组可以表示数据库记录,其组件可以表示记录的各个字段。

  • 为了提供对数据集的轻松访问和操作。 下面的示例定义了一组 Tuple<T1,T2,T3,T4,T5> 对象,这些对象包含在美国足球中跑回的名称、他们踢的比赛次数、携带次数、获得的总码数以及在这些比赛中得分的触地数。 该数组传递给 ComputeStatistics 该方法,该方法计算每个运行后每个游戏的携带次数、每个游戏的平均码数、每个携带的平均码数,以及每个尝试的平均触地数。

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          // Organization of runningBacks 5-tuple:
          //    Component 1: Player name
          //    Component 2: Number of games played
          //    Component 3: Number of attempts (carries)
          //    Component 4: Number of yards gained 
          //    Component 5: Number of touchdowns   
          Tuple<string, int, int, int, int>[] runningBacks =
               { Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),  
                 Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),            
                 Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),            
                 Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),            
                 Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) }; 
          // Calculate statistics.
          // Organization of runningStats 5-tuple:
          //    Component 1: Player name
          //    Component 2: Number of attempts per game
          //    Component 3: Number of yards per game
          //    Component 4: Number of yards per attempt 
          //    Component 5: Number of touchdowns per attempt   
          Tuple<string, double, double, double, double>[] runningStats  = 
              ComputeStatistics(runningBacks);
    
          // Display the result.          
          Console.WriteLine("{0,-16} {1,5} {2,6} {3,7} {4,7} {5,7} {6,7} {7,5} {8,7}\n", 
                            "Name", "Games", "Att", "Att/Gm", "Yards", "Yds/Gm",
                            "Yds/Att", "TD", "TD/Att");
          for (int ctr = 0; ctr < runningBacks.Length; ctr++)
             Console.WriteLine("{0,-16} {1,5} {2,6:N0} {3,7:N1} {4,7:N0} {5,7:N1} {6,7:N2} {7,5} {8,7:N3}\n", 
                               runningBacks[ctr].Item1, runningBacks[ctr].Item2, runningBacks[ctr].Item3, 
                               runningStats[ctr].Item2, runningBacks[ctr].Item4, runningStats[ctr].Item3, 
                               runningStats[ctr].Item4, runningBacks[ctr].Item5, runningStats[ctr].Item5);
       }
    
       private static Tuple<string, double, double, double, double>[] ComputeStatistics(
                    Tuple<string, int, int, int, int>[] players) 
       {
          Tuple<string, double, double, double, double> result; 
          var list = new List<Tuple<string, double, double, double, double>>();
          
          foreach (var player in players)
          {
             // Create result object containing player name and statistics.
             result = Tuple.Create(player.Item1,  
                                   player.Item3/((double)player.Item2), 
                                   player.Item4/((double)player.Item2),
                                   player.Item4/((double)player.Item3), 
                                   player.Item5/((double)player.Item3));
             list.Add(result);         
          }
          return list.ToArray();  
       }
    }
    // The example displays the following output:
    //    Name             Games    Att  Att/Gm   Yards  Yds/Gm Yds/Att    TD  TD/Att
    //    
    //    Payton, Walter     190  3,838    20.2  16,726    88.0    4.36   110   0.029
    //    
    //    Sanders, Barry     153  3,062    20.0  15,269    99.8    4.99    99   0.032
    //    
    //    Brown, Jim         118  2,359    20.0  12,312   104.3    5.22   106   0.045
    //    
    //    Dickerson, Eric    144  2,996    20.8  13,259    92.1    4.43    90   0.030
    //    
    //    Faulk, Marshall    176  2,836    16.1  12,279    69.8    4.33   100   0.035
    
    open System
    
    let computeStatistics (players: Tuple<string, int, int, int, int>[]) = 
        [| for player in players do
            // Create result object containing player name and statistics.
            Tuple.Create(player.Item1,  
                         double player.Item3 / double player.Item2,
                         double player.Item4 / double player.Item2,
                         double player.Item4 / double player.Item3, 
                         double player.Item5 / double player.Item3) |]
    
    
    // Organization of runningBacks 5-tuple:
    //    Component 1: Player name
    //    Component 2: Number of games played
    //    Component 3: Number of attempts (carries)
    //    Component 4: Number of yards gained 
    //    Component 5: Number of touchdowns   
    let runningBacks =
        [| Tuple.Create("Payton, Walter", 190, 3838, 16726, 110)
           Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99)
           Tuple.Create("Brown, Jim", 118, 2359, 12312, 106)
           Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90)
           Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) |]
    // Calculate statistics.
    // Organization of runningStats 5-tuple:
    //    Component 1: Player name
    //    Component 2: Number of attempts per game
    //    Component 3: Number of yards per game
    //    Component 4: Number of yards per attempt 
    //    Component 5: Number of touchdowns per attempt   
    let runningStats = computeStatistics runningBacks
    
    // Display the result.          
    printfn "%-16s %5s %6s %7s %7s %7s %7s %5s %7s\n" "Name" "Games" "Att" "Att/Gm" "Yards" "Yds/Gm" "Yds/Att" "TD" "TD/Att"
    for i = 0 to runningBacks.Length - 1 do
        printfn $"{runningBacks[i].Item1,-16} {runningBacks[i].Item2,5} {runningBacks[i].Item3,6:N0} {runningBacks[i].Item2,7:N1} {runningBacks[i].Item4,7:N0} {runningBacks[i].Item3,7:N1} {runningBacks[i].Item4,7:N2} {runningBacks[i].Item5,5} {runningBacks[i].Item5,7:N3}\n" 
    
    // The example displays the following output:
    //    Name             Games    Att  Att/Gm   Yards  Yds/Gm Yds/Att    TD  TD/Att
    //    
    //    Payton, Walter     190  3,838    20.2  16,726    88.0    4.36   110   0.029
    //    
    //    Sanders, Barry     153  3,062    20.0  15,269    99.8    4.99    99   0.032
    //    
    //    Brown, Jim         118  2,359    20.0  12,312   104.3    5.22   106   0.045
    //    
    //    Dickerson, Eric    144  2,996    20.8  13,259    92.1    4.43    90   0.030
    //    
    //    Faulk, Marshall    176  2,836    16.1  12,279    69.8    4.33   100   0.035
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          ' Organization of runningBacks 5-tuple:
          '    Component 1: Player name
          '    Component 2: Number of games played
          '    Component 3: Number of attempts (carries)
          '    Component 4: Number of yards gained 
          '    Component 5: Number of touchdowns   
          Dim runningBacks() =
              { Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),  
                Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),            
                Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),            
                Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),            
                Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) } 
          ' Calculate statistics.
          ' Organization of runningStats 5-tuple:
          '    Component 1: Player name
          '    Component 2: Number of attempts per game
          '    Component 3: Number of yards per game
          '    Component 4: Number of yards per attempt 
          '    Component 5: Number of touchdowns per attempt   
          Dim runningStats() = ComputeStatistics(runningBacks)
    
          ' Display the result.          
          Console.WriteLine("{0,-16} {1,5} {2,6} {3,7} {4,7} {5,7} {6,7} {7,5} {8,7}", 
                            "Name", "Games", "Att", "Att/Gm", "Yards", "Yds/Gm",
                            "Yds/Att", "TD", "TD/Att")
          Console.WriteLine()
          For ctr As Integer = 0 To runningBacks.Length - 1
             Console.WriteLine("{0,-16} {1,5} {2,6:N0} {3,7:N1} {4,7:N0} {5,7:N1} {6,7:N2} {7,5} {8,7:N3}", 
                               runningBacks(ctr).Item1, runningBacks(ctr).Item2, runningBacks(ctr).Item3, 
                               runningStats(ctr).Item2, runningBacks(ctr).Item4, runningStats(ctr).Item3, 
                               runningStats(ctr).Item4, runningBacks(ctr).Item5, runningStats(ctr).Item5)
             Console.WriteLine()  
          Next     
       End Sub
    
       Private Function ComputeStatistics(players() As Tuple(Of String, Integer, Integer, Integer, Integer)) _
                        As Tuple(Of String, Double, Double, Double, Double)()
    
          Dim result As Tuple(Of String, Double, Double, Double, Double)
          Dim list As New List(Of Tuple(Of String, Double, Double, Double, Double))()
          
          For Each player In players
             ' Create result object containing player name and statistics.
             result = Tuple.Create(player.Item1,  
                                player.Item3/player.Item2, player.Item4/player.Item2,
                                player.Item4/player.Item3, player.Item5/player.Item3)
             list.Add(result)         
          Next
          Return list.ToArray()  
       End Function
    End Module
    ' The example displays the following output:
    '    Name             Games    Att  Att/Gm   Yards  Yds/Gm Yds/Att    TD  TD/Att
    '    
    '    Payton, Walter     190  3,838    20.2  16,726    88.0    4.36   110   0.029
    '    
    '    Sanders, Barry     153  3,062    20.0  15,269    99.8    4.99    99   0.032
    '    
    '    Brown, Jim         118  2,359    20.0  12,312   104.3    5.22   106   0.045
    '    
    '    Dickerson, Eric    144  2,996    20.8  13,259    92.1    4.43    90   0.030
    '    
    '    Faulk, Marshall    176  2,836    16.1  12,279    69.8    4.33   100   0.035
    
  • 从方法返回多个值,而不使用 out C#) 中的参数 (或ByRefVisual Basic) 中的参数 (。 例如,上一个示例在对象数组 Tuple<T1,T2,T3,T4,T5> 中返回其计算统计信息以及玩家的名称。

  • 通过单个参数将多个值传递给方法。 例如,该方法 Thread.Start(Object) 有一个参数,可用于向线程在启动时执行的方法提供一个值。 如果将对象作为方法参数提供 Tuple<T1,T2,T3,T4,T5> ,则可以提供线程的启动例程,其中包含五个项目的数据。

构造函数

Tuple<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5)

初始化 Tuple<T1,T2,T3,T4,T5> 类的新实例。

属性

Item1

获取当前 Tuple<T1,T2,T3,T4,T5> 对象的第一个分量的值。

Item2

获取当前 Tuple<T1,T2,T3,T4,T5> 对象的第二个分量的值。

Item3

获取当前 Tuple<T1,T2,T3,T4,T5> 对象的第三个分量的值。

Item4

获取当前 Tuple<T1,T2,T3,T4,T5> 对象的第四个分量的值。

Item5

获取当前 Tuple<T1,T2,T3,T4,T5> 对象的第五个分量的值。

方法

Equals(Object)

返回一个值,该值指示当前的 Tuple<T1,T2,T3,T4,T5> 对象是否与指定对象相等。

GetHashCode()

返回当前 Tuple<T1,T2,T3,T4,T5> 对象的哈希代码。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示此 Tuple<T1,T2,T3,T4,T5> 实例的值的字符串。

显式接口实现

IComparable.CompareTo(Object)

比较当前 Tuple<T1,T2,T3,T4,T5> 对象与指定对象,并返回一个整数,该整数指示当前对象在排序顺序中的位置:是在指定对象之前、之后还是在与指定对象相同的位置。

IStructuralComparable.CompareTo(Object, IComparer)

使用指定的比较器将当前的 Tuple<T1,T2,T3,T4,T5> 对象与指定对象进行比较,并返回一个整数,该整数指示当前对象在排序顺序中的位置是在指定对象之前、之后还是与其相同。

IStructuralEquatable.Equals(Object, IEqualityComparer)

返回一个值,该值根据指定的比较方法指示当前的 Tuple<T1,T2,T3,T4,T5> 对象是否与指定对象相等。

IStructuralEquatable.GetHashCode(IEqualityComparer)

使用指定的计算方法计算当前 Tuple<T1,T2,T3,T4,T5> 对象的哈希代码。

ITuple.Item[Int32]

获取指定 Tuple 元素的值。

ITuple.Length

获取 Tuple 中的元素数。

扩展方法

Deconstruct<T1,T2,T3,T4,T5>(Tuple<T1,T2,T3,T4,T5>, T1, T2, T3, T4, T5)

将具有 5 个元素的元组解构为不同的变量。

ToValueTuple<T1,T2,T3,T4,T5>(Tuple<T1,T2,T3,T4,T5>)

Tuple 类的实例转换为 ValueTuple 结构的实例。

适用于

另请参阅