Tuple<T1,T2,T3,T4> Klasa

Definicja

Reprezentuje krotkę 4-krotkową lub czteroosobową.

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

Parametry typu

T1

Typ pierwszego składnika spójnej kolekcji.

T2

Typ drugiego składnika spójnej kolekcji.

T3

Typ trzeciego składnika spójnej kolekcji.

T4

Typ czwartego składnika spójnej kolekcji.

Dziedziczenie
Tuple<T1,T2,T3,T4>
Atrybuty
Implementuje

Uwagi

Spójna kolekcja to struktura danych, która ma określoną liczbę i kolejność wartości. Klasa Tuple<T1,T2,T3,T4> reprezentuje krotkę 4-krotkową lub czterokrotnie, która jest krotką zawierającą cztery składniki.

Wystąpienie obiektu można utworzyć Tuple<T1,T2,T3,T4> , wywołując Tuple<T1,T2,T3,T4> konstruktor lub metodę statyczną Tuple.Create<T1,T2,T3,T4>(T1, T2, T3, T4) . Wartość składników krotki można pobrać przy użyciu właściwości wystąpienia tylko Item1do odczytu, Item2, Item3i Item4 .

Spójne kolekcje są powszechnie wykorzystywane na cztery różne sposoby:

  • Reprezentowanie jednego zestawu danych. Na przykład spójna kolekcja może reprezentować rekord bazy danych, a jej składniki mogą reprezentować poszczególne pola rekordu.

  • Zapewnienie łatwego dostępu do zestawu danych i możliwości wykonywania w nim różnych operacji. W poniższym przykładzie zdefiniowano tablicę Tuple<T1,T2,T3,T4> obiektów, które zawierają nazwy dzbanów baseballowych, liczbę rund, które rozbiły, oraz liczbę uzyskanych przebiegów (przebiegów, które zdobyły bez błędów fieldingu) i trafień, które zrezygnowały. Tablica jest przekazywana do ComputeStatistics metody, która oblicza średnią przebiegu zarobionego dzbana (średnia liczba przebiegów zrezygnowanych w grze dziewięcio inningu) i średnia liczba trafień zrezygnowanych na inning. Metoda używa również tych dwóch średnich do obliczenia hipotetycznej średniej skuteczności.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          Tuple<string, decimal, int, int>[] pitchers  =  
               { Tuple.Create("McHale, Joe", 240.1m, 221, 96),
                 Tuple.Create("Paul, Dave", 233.1m, 231, 84), 
                 Tuple.Create("Williams, Mike", 193.2m, 183, 86),
                 Tuple.Create("Blair, Jack", 168.1m, 146, 65), 
                 Tuple.Create("Henry, Walt", 140.1m, 96, 30),
                 Tuple.Create("Lee, Adam", 137.2m, 109, 45),
                 Tuple.Create("Rohr, Don", 101.0m, 110, 42) };
          Tuple<string, double, double, double>[] results= ComputeStatistics(pitchers);
    
          // Display the results.
          Console.WriteLine("{0,-20} {1,9} {2,11} {3,15}\n", 
                            "Pitcher", "ERA", "Hits/Inn.", "Effectiveness");
          foreach (var result in results)
             Console.WriteLine("{0,-20} {1,9:F2} {2,11:F2} {3,15:F2}",  
                            result.Item1, result.Item2, result.Item3, result.Item4);
       }
    
       private static Tuple<string, double, double, double>[] ComputeStatistics(Tuple<string, decimal, int, int>[] pitchers)
       {    
          var list = new List<Tuple<string, double, double, double>>();
          Tuple<string, double, double, double> result;
    
          foreach (var pitcher in pitchers)
          {
             // Decimal portion of innings pitched represents 1/3 of an inning
             double innings = (double) Math.Truncate(pitcher.Item2);
             innings = innings + (((double)pitcher.Item2 - innings) * .33);
             
             double ERA = pitcher.Item4/innings * 9;
             double hitsPerInning = pitcher.Item3/innings;
             double EI = (ERA * 2 + hitsPerInning * 9)/3;
             result = new Tuple<string, double, double, double>
                               (pitcher.Item1, ERA, hitsPerInning, EI);
             list.Add(result);
          }
          return list.ToArray();
       }
    }
    // The example displays the following output;
    //       Pitcher                    ERA   Hits/Inn.   Effectiveness
    //       
    //       McHale, Joe               3.60        0.92            5.16
    //       Paul, Dave                3.24        0.99            5.14
    //       Williams, Mike            4.01        0.95            5.52
    //       Blair, Jack               3.48        0.87            4.93
    //       Henry, Walt               1.93        0.69            3.34
    //       Lee, Adam                 2.95        0.80            4.36
    //       Rohr, Don                 3.74        1.09            5.76
    
    open System
    
    let computeStatistics (pitchers: Tuple<string, decimal, int, int>[]) =
        [| for pitcher in pitchers do
            // Decimal portion of innings pitched represents 1/3 of an inning
            let innings =  truncate (double pitcher.Item2) |> double
            let innings = innings + (double pitcher.Item2 - innings) * 0.33
            
            let ERA = double pitcher.Item4 / innings * 9.
            let hitsPerInning = double pitcher.Item3 / innings
            let EI = (ERA * 2. + hitsPerInning * 9.) / 3.
            Tuple<string, double, double, double>(pitcher.Item1, ERA, hitsPerInning, EI)|]
    
    let pitchers  =  
        [| Tuple.Create("McHale, Joe", 240.1m, 221, 96)
           Tuple.Create("Paul, Dave", 233.1m, 231, 84)
           Tuple.Create("Williams, Mike", 193.2m, 183, 86)
           Tuple.Create("Blair, Jack", 168.1m, 146, 65) 
           Tuple.Create("Henry, Walt", 140.1m, 96, 30)
           Tuple.Create("Lee, Adam", 137.2m, 109, 45)
           Tuple.Create("Rohr, Don", 101.0m, 110, 42) |]
    
    let results = computeStatistics pitchers
    
    // Display the results.
    printfn "%-20s %9s %11s %15s\n" "Pitcher" "ERA" "Hits/Inn." "Effectiveness"
    for result in results do
        printfn $"{result.Item1,-20} {result.Item2,9:F2} {result.Item3,11:F2} {result.Item4,15:F2}"
    
    // The example displays the following output
    //       Pitcher                    ERA   Hits/Inn.   Effectiveness
    //       
    //       McHale, Joe               3.60        0.92            5.16
    //       Paul, Dave                3.24        0.99            5.14
    //       Williams, Mike            4.01        0.95            5.52
    //       Blair, Jack               3.48        0.87            4.93
    //       Henry, Walt               1.93        0.69            3.34
    //       Lee, Adam                 2.95        0.80            4.36
    //       Rohr, Don                 3.74        1.09            5.76
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim pitchers() =  
                   { Tuple.Create("McHale, Joe", 240.1d, 221, 96),
                     Tuple.Create("Paul, Dave", 233.1d, 231, 84), 
                     Tuple.Create("Williams, Mike", 193.2d, 183, 86),
                     Tuple.Create("Blair, Jack", 168.1d, 146, 65), 
                     Tuple.Create("Henry, Walt", 140.1d, 96, 30),
                     Tuple.Create("Lee, Adam", 137.2d, 109, 45),
                     Tuple.Create("Rohr, Don", 101.0d, 110, 42) }
          Dim results() = ComputeStatistics(pitchers)
    
          ' Display the results.
          Console.WriteLine("{0,-20} {1,9} {2,11} {3,15}", "Pitcher", "ERA", "Hits/Inn.", "Effectiveness")
          Console.WriteLine()
          For Each result In results
             Console.WriteLine("{0,-20} {1,9:F2} {2,11:F2} {3,15:F2}",  
                            result.Item1, result.Item2, result.Item3, result.Item4)
          Next
       End Sub
       
       Private Function ComputeStatistics(pitchers() As Tuple(Of String, Decimal, Integer, Integer)) _ 
                                    As Tuple(Of String, Double, Double, Double)()
          Dim list As New List(Of Tuple(Of String, Double, Double, Double))
          Dim result As Tuple(Of String, Double, Double, Double)
    
          For Each pitcher As Tuple(Of String, Decimal, Integer, Integer) In pitchers
             ' Decimal portion of innings pitched represents 1/3 of an inning
             Dim innings As Double = CDbl(Math.Truncate(pitcher.Item2))
             innings = innings + ((pitcher.Item2 - innings) * .33)
             
             Dim ERA As Double = pitcher.Item4/innings * 9
             Dim hitsPerInning As Double = pitcher.Item3/innings
             Dim EI As Double = (ERA * 2 + hitsPerInning * 9)/3
             result = New Tuple(Of String, Double, Double, Double) _
                               (pitcher.Item1, ERA, hitsPerInning, EI)
             list.Add(result) 
          Next
          Return list.ToArray()
       End Function
    End Module
    ' The example displays the following output:
    '       Pitcher                    ERA   Hits/Inn.   Effectiveness
    '       
    '       McHale, Joe               3.60        0.92            5.16
    '       Paul, Dave                3.24        0.99            5.14
    '       Williams, Mike            4.01        0.95            5.52
    '       Blair, Jack               3.48        0.87            4.93
    '       Henry, Walt               1.93        0.69            3.34
    '       Lee, Adam                 2.95        0.80            4.36
    '       Rohr, Don                 3.74        1.09            5.76
    
  • Aby zwrócić wiele wartości z metody bez użycia parametrów out (w języku C#) lub ByRef parametrów (w Visual Basic). Na przykład poprzedni przykład zwraca obliczone statystyki wraz z nazwą dzbana w tablicy Tuple<T1,T2,T3,T4> obiektów.

  • Przekazywanie wielu wartości do metody za pomocą jednego parametru. Na przykład Thread.Start(Object) metoda ma jeden parametr, który umożliwia podanie jednej wartości do metody wykonywanej przez wątek podczas uruchamiania. Jeśli podasz Tuple<T1,T2,T3,T4> obiekt jako argument metody, możesz podać procedurę uruchamiania wątku z czterema elementami danych.

Konstruktory

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

Inicjuje nowe wystąpienie klasy Tuple<T1,T2,T3,T4>.

Właściwości

Item1

Pobiera wartość pierwszego składnika bieżącego Tuple<T1,T2,T3,T4> obiektu.

Item2

Pobiera wartość drugiego składnika bieżącego Tuple<T1,T2,T3,T4> obiektu.

Item3

Pobiera wartość trzeciego składnika bieżącego Tuple<T1,T2,T3,T4> obiektu.

Item4

Pobiera wartość czwartego składnika bieżącego Tuple<T1,T2,T3,T4> obiektu.

Metody

Equals(Object)

Zwraca wartość wskazującą, czy bieżący Tuple<T1,T2,T3,T4> obiekt jest równy określonemu obiektowi.

GetHashCode()

Zwraca kod skrótu dla bieżącego Tuple<T1,T2,T3,T4> obiektu.

GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący wartość tego Tuple<T1,T2,T3,T4> wystąpienia.

Jawne implementacje interfejsu

IComparable.CompareTo(Object)

Porównuje bieżący Tuple<T1,T2,T3,T4> obiekt z określonym obiektem i zwraca liczbę całkowitą wskazującą, czy bieżący obiekt znajduje się przed, po, czy w tej samej pozycji co określony obiekt w kolejności sortowania.

IStructuralComparable.CompareTo(Object, IComparer)

Porównuje bieżący Tuple<T1,T2,T3,T4> obiekt z określonym obiektem przy użyciu określonego porównania i zwraca liczbę całkowitą, która wskazuje, czy bieżący obiekt jest przed, po, czy w tej samej pozycji, co określony obiekt w kolejności sortowania.

IStructuralEquatable.Equals(Object, IEqualityComparer)

Zwraca wartość wskazującą, czy bieżący Tuple<T1,T2,T3,T4> obiekt jest równy określonemu obiektowi na podstawie określonej metody porównania.

IStructuralEquatable.GetHashCode(IEqualityComparer)

Oblicza kod skrótu dla bieżącego Tuple<T1,T2,T3,T4> obiektu przy użyciu określonej metody obliczeniowej.

ITuple.Item[Int32]

Pobiera wartość określonego Tuple elementu.

ITuple.Length

Pobiera liczbę elementów w elemecie Tuple.

Metody rozszerzania

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

Dekonstruktoruje krotkę z 4 elementami na oddzielne zmienne.

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

Konwertuje wystąpienie Tuple klasy na wystąpienie ValueTuple struktury.

Dotyczy

Zobacz też