Tuple クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
タプル オブジェクトを作成するための静的メソッドを提供します。
public ref class Tuple abstract sealed
public static class Tuple
type Tuple = class
Public Class Tuple
- 継承
-
Tuple
例
次の例では、20 未満の素数を含む 8 タプル (8 進数) を作成します。
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
注釈
タプルは、要素の特定の数とシーケンスを持つデータ構造です。 タプルの例として、3 つの要素 (3 タプルまたは 3 組と呼ばれます) を持つデータ構造があります。これは、最初の要素に人物の名前、2 番目の要素に年、3 番目の要素でその年の人の収入などの識別子を格納するために使用されます。 .NET Framework は、1 ~ 7 個の要素を持つタプルを直接サポートします。 さらに、Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> オブジェクトの Rest プロパティにタプル オブジェクトを入れ子にすることで、8 つ以上の要素のタプルを作成できます。
タプルは、一般的に次の 4 つの方法で使用されます。
1 つのデータ セットを表す。 たとえば、タプルはデータベース レコードを表し、そのコンポーネントはレコードの個々のフィールドを表すことができます。
データ セットへの簡単なアクセスと操作を提供します。
outパラメーター (C#) またはByRefパラメーター (Visual Basic の場合) を使用せずに、メソッドから複数の値を返すには。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 タプルまたはセプチュプレを作成します。 |
| 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 タプルまたはペアを作成します。 |
| Create<T1>(T1) |
新しい 1 タプルまたはシングルトンを作成します。 |