タプルを == 演算子で比較、Equals() で比較
using System; class Program { static void Main() { var a = Tuple.Create(1, 2); var b = Tuple.Create(1, 2); Console.WriteLine(a == b); // False Console.WriteLine(a.Equals(b)); // True } }
実行結果です。
False True
タプルを Dictionary のキーにしてみる
using System; using System.Collections.Generic; class Program { static void Main() { var a = Tuple.Create(1, 2); var b = Tuple.Create(1, 2); var dict = new Dictionary<Tuple<int, int>, int>(); dict[a] = 12; Console.WriteLine(dict[a]); // 12 dict[b] = 30; Console.WriteLine(dict[a]); // 30 Console.WriteLine(dict[b]); // 30 } }
実行結果です。
12 30 30
タプルを HashSet に格納
using System; using System.Collections.Generic; class Program { static void Main() { var a = Tuple.Create(1, 2); var b = Tuple.Create(1, 2); var hs = new HashSet<Tuple<int, int>>(); hs.Add(a); Console.WriteLine(hs.Contains(b)); // True } }
実行結果です。
True