Tuple クラス

定義

タプル オブジェクトを作成するための静的メソッドを提供します。

public ref class Tuple abstract sealed
public static class Tuple
type Tuple = class
Public Class Tuple
継承
Tuple

次の例では、20 未満の素数を含む 8 タプル (octuple) を作成します。

var primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19);
Console.WriteLine("Prime numbers less than 20: " + 
                  "{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7}",
                  primes.Item1, primes.Item2, primes.Item3, 
                  primes.Item4, primes.Item5, primes.Item6,
                  primes.Item7, primes.Rest.Item1);
// The example displays the following output:
//    Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
open System

let primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19)
printfn $"Prime numbers less than 20: {primes.Item1}, {primes.Item2}, {primes.Item3}, {primes.Item4}, {primes.Item5}, {primes.Item6}, {primes.Item7}, and {primes.Rest.Item1}"
//    Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
Dim primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19)
Console.WriteLine("Prime numbers less than 20: " + 
                  "{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7}",
                  primes.Item1, primes.Item2, primes.Item3, 
                  primes.Item4, primes.Item5, primes.Item6,
                  primes.Item7, primes.Rest.Item1)
' The example displays the following output:
'     Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19

注釈

タプルは、要素の特定の数とシーケンスを持つデータ構造です。 タプルの例としては、最初の要素に人物の名前、2 番目の要素に年、3 番目の要素でその年の人の収入などの識別子を格納するために使用される 3 つの要素 (3 タプルまたはトリプルと呼ばれます) を含むデータ構造があります。 .NET Frameworkは、1 から 7 個の要素を持つタプルを直接サポートします。 さらに、オブジェクトの プロパティにタプル オブジェクトを入れ子にすることで、8 つ以上の要素のタプルをRestTuple<T1,T2,T3,T4,T5,T6,T7,TRest>作成できます。

タプルは、一般的に次の 4 つの方法で使用されます。

  • 1 つのデータ セットを表す。 たとえば、タプルはデータベース レコードを表し、そのコンポーネントはレコードの個々のフィールドを表すことができます。

  • データ セットへの簡単なアクセスと操作を提供します。

  • パラメーター (C#では) またはByRefパラメーター (Visual Basic では) を使用outせずにメソッドから複数の値を返すには。

  • 1 つのパラメーターを使用して複数の値をメソッドに渡す。 たとえば、 Thread.Start(Object) メソッドには、起動時にスレッドが実行するメソッドに 1 つの値を指定できる 1 つのパラメーターがあります。 メソッド引数として オブジェクトを Tuple<T1,T2,T3> 指定する場合は、スレッドのスタートアップ ルーチンに 3 つのデータ項目を指定できます。

クラス自体は Tuple タプルを表しません。 代わりに、.NET Frameworkでサポートされているタプル型のインスタンスを作成するための静的メソッドを提供するクラスです。 これは、各タプル コンポーネントの型を明示的に指定しなくてもタプル オブジェクトをインスタンス化するために呼び出すことができるヘルパー メソッドを提供します。

タプル クラスのインスタンスは、そのクラス コンストラクターを呼び出すことで作成できますが、これを行うコードは面倒な場合があります。 次の例では、クラス コンストラクターを使用して、1950 年から 2000 年の各国勢調査のニューヨーク市の人口データを含む 7 タプルまたはセプチュプラを作成します。

// Create a 7-tuple.
var population = new Tuple<string, int, int, int, int, int, int>(
                           "New York", 7891957, 7781984, 
                           7894862, 7071639, 7322564, 8008278);
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
                  population.Item1, population.Item7);
// The example displays the following output:
//       Population of New York in 2000: 8,008,278
// Create a 7-tuple.
let population = Tuple<string, int, int, int, int, int, int>(
                            "New York", 7891957, 7781984, 
                            7894862, 7071639, 7322564, 8008278)
// Display the first and last elements.
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
// The example displays the following output:
//       Population of New York in 2000: 8,008,278
' Create a 7-tuple.
Dim population As New Tuple(Of String, Integer, Integer, Integer, Integer, Integer, Integer) _
                           ("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
' Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
                  population.Item1, population.Item7)
' The example displays the following output:
'        Population of New York in 2000: 8,008,278

次の例に示すように、ヘルパー メソッドを使用して同じタプル オブジェクトを作成する方が簡単です。

// Create a 7-tuple.
var population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
                  population.Item1, population.Item7);
// The example displays the following output:
//       Population of New York in 2000: 8,008,278
// Create a 7-tuple.
let population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
// Display the first and last elements.
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
// The example displays the following output:
//       Population of New York in 2000: 8,008,278
' Create a 7-tuple.
Dim population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
' Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
                  population.Item1, population.Item7)
' The example displays the following output:
'        Population of New York in 2000: 8,008,278

ヘルパー メソッドは Create 、1 から 8 個のコンポーネント (つまり、1 から 8 個までのシングルトン) を持つタプル オブジェクトの作成を直接サポートします。 タプルに含まれる可能性のあるコンポーネントの数に実際的な制限はありませんが、ヘルパー メソッドを使用して 9 つ以上のコンポーネントを含むタプルを作成することはできません。 このようなタプルを作成するには、 コンストラクターを呼び出す Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 必要があります。

注意

タプルを使用する追加情報と例については、.NET Frameworkの個々のタプル型のドキュメントを参照してください。 これらは、このトピックの最後にある「関連項目」セクションに記載されています。

メソッド

Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8)

新しい 8 組 (8 つの要素で構成される組) を作成します。

Create<T1,T2,T3,T4,T5,T6,T7>(T1, T2, T3, T4, T5, T6, T7)

新しい 7 タプル (7 つの要素で構成されるタプル) を作成します。

Create<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6)

新しい 6 タプル (6 つの要素で構成されるタプル) 作成します。

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

新しい 5 タプル (5 つの要素で構成されるタプル) を作成します。

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

新しい 4 タプル (4 つの要素で構成されるタプル) を作成します。

Create<T1,T2,T3>(T1, T2, T3)

新しい 3 タプル (3 つの要素で構成されるタプル) を作成します。

Create<T1,T2>(T1, T2)

新しい 2 組 (2 つの要素で構成される組) を作成します。

Create<T1>(T1)

新しい 1 タプル (1 つの要素で構成される組) を作成します。

適用対象

こちらもご覧ください