C#で6要素タプル(6-Tuple)を作成する方法
C#のTuple<T1, T2, T3, T4, T5, T6>クラスは、6つの要素を持つ「6タプル」を表します。タプルとは、異なる型の値をひとまとまりのデータとして扱うことができるデータ構造です。
6タプルが持つプロパティ
Tuple<T1, T2, T3, T4, T5, T6>クラスには、各要素に対応する以下の6つのプロパティが用意されています。
Item1 ― 現在のTuple<T1, T2, T3, T4, T5, T6>オブジェクトの1番目の要素の値を取得します。
Item2 ― 現在のオブジェクトの2番目の要素の値を取得します。
Item3 ― 現在のオブジェクトの3番目の要素の値を取得します。
Item4 ― 現在のオブジェクトの4番目の要素の値を取得します。
Item5 ― 現在のオブジェクトの5番目の要素の値を取得します。
Item6 ― 現在のオブジェクトの6番目の要素の値を取得します。
例1:文字列と数値を混在させた6タプル
それでは、C#で6タプルを実装する具体的な例を見てみましょう。ここでは、文字列型とint型を組み合わせたタプルを作成し、各プロパティから値を取得しています。
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string,int,string,int,int,string> tuple = new Tuple<string,int,string,int,int,string>("jack", 150, "pete", 300, 600, "allan");
Console.WriteLine("Value (Item1)= " + tuple.Item1);
Console.WriteLine("Value (Item2)= " + tuple.Item2);
Console.WriteLine("Value (Item3)= " + tuple.Item3);
Console.WriteLine("Value (Item4)= " + tuple.Item4);
Console.WriteLine("Value (Item5)= " + tuple.Item5);
Console.WriteLine("Value (Item6)= " + tuple.Item6);
if (tuple.Item1 == "kevin") {
Console.WriteLine("Exists: Tuple Item 1 = " +tuple.Item1);
}
if (tuple.Item2 == 250) {
Console.WriteLine("Exists: Tuple Item 2 = " +tuple.Item2);
}
if (tuple.Item3 == "pete") {
Console.WriteLine("Exists: Tuple Item 3 = " +tuple.Item3);
}
if (tuple.Item4 == 300) {
Console.WriteLine("Exists: Tuple Item 4 = " +tuple.Item4);
}
if (tuple.Item5 == 400) {
Console.WriteLine("Exists: Tuple Item 5 = " +tuple.Item5);
}
if (tuple.Item6 == "allan") {
Console.WriteLine("Exists: Tuple Item 6 = " +tuple.Item6);
}
}
}
出力結果
上記のコードを実行すると、次のような出力が得られます。条件に一致したItem3、Item4、Item6のみ「Exists」メッセージが表示されている点に注目してください。
Value (Item1)= jack Value (Item2)= 150 Value (Item3)= pete Value (Item4)= 300 Value (Item5)= 600 Value (Item6)= allan Exists: Tuple Item 3 = pete Exists: Tuple Item 4 = 300 Exists: Tuple Item 6 = allan
例2:すべてint型の6タプル
次に、すべての要素がint型である6タプルの例を見てみましょう。タプルは同じ型の要素だけで構成することもできます。
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int,int,int,int,int,int> tuple = new Tuple<int,int,int,int,int,int>(100, 150, 200, 300, 600, 1000);
Console.WriteLine("Value (Item1)= " + tuple.Item1);
Console.WriteLine("Value (Item2)= " + tuple.Item2);
Console.WriteLine("Value (Item3)= " + tuple.Item3);
Console.WriteLine("Value (Item4)= " + tuple.Item4);
Console.WriteLine("Value (Item5)= " + tuple.Item5);
Console.WriteLine("Value (Item6)= " + tuple.Item6);
if (tuple.Item1 == 100) {
Console.WriteLine("Exists: Tuple Item 1 = " +tuple.Item1);
}
if (tuple.Item2 == 250) {
Console.WriteLine("Exists: Tuple Item 2 = " +tuple.Item2);
}
if (tuple.Item3 == 300) {
Console.WriteLine("Exists: Tuple Item 3 = " +tuple.Item3);
}
if (tuple.Item4 == 300) {
Console.WriteLine("Exists: Tuple Item 4 = " +tuple.Item4);
}
if (tuple.Item5 == 400) {
Console.WriteLine("Exists: Tuple Item 5 = " +tuple.Item5);
}
if (tuple.Item6 == 500) {
Console.WriteLine("Exists: Tuple Item 6 = " +tuple.Item6);
}
}
}
出力結果
このコードを実行すると、以下の出力が表示されます。ここでは条件に一致したItem1とItem4についてのみ「Exists」メッセージが出力されます。
Value (Item1)= 100 Value (Item2)= 150 Value (Item3)= 200 Value (Item4)= 300 Value (Item5)= 600 Value (Item6)= 1000 Exists: Tuple Item 1 = 100 Exists: Tuple Item 4 = 300
補足:ValueTupleとの違い
なお、C# 7.0以降では、より軽量な値型のタプルであるValueTupleも利用できます。ValueTupleを使うと("jack", 150, "pete", 300, 600, "allan")のように簡潔な構文でタプルを生成でき、要素に任意の名前を付けることも可能です。従来の参照型であるTupleクラスと使い分けることで、目的に応じた効率的なコードが書けます。
-
Pythonで空のタプルを作成する方法をわかりやすく解説
Pythonでは、代入文において括弧 () の中に要素を何も書かないことで、空のタプル(tuple)オブジェクトを作成できます。また、組み込み関数 tuple() を引数なしで呼び出すことでも、空のタプルを生成することが可能です。 空のタプルを作成する2つの方法 1. 括弧 () を使う方法 最もシンプルな方法は、要素を含まない括弧をそのまま変数に代入することです。 >>> T1 = () >>> T1 () 2. tuple() 関数を使う方法 組み込み関数 tuple() に引数を渡さずに呼び出しても、同様に空のタプルが作成されます。 >>&
-
Apple IDの作成方法を完全解説|子ども用アカウントや2ファクタ認証の設定まで
Apple製品やサービスを使いこなすうえで、Apple IDは欠かせない存在です。幸い、その作成はとても簡単。この記事では、自分用・お子様用のApple IDの作り方から、強固なセキュリティ設定、ファミリー共有グループの始め方まで、わかりやすく解説します。 Apple IDを作成する手順 Apple IDは、Apple公式サイトのApple IDページ(appleid.apple.com)にアクセスし、「Apple IDを作成」をクリックするだけで登録できます。 appleid.apple.com は、Apple IDの作成と管理を行うためのオンライン窓口です Apple IDの登録にあたっ