LoginSignup
2
4

More than 5 years have passed since last update.

TupleでDictionaryの複合キーを実現するサンプル

Posted at
TupleSample.cs
    class TupleSample {
        static void Main(string[] args) {
            var dic = new Dictionary<Tuple<int, int>, string>();
            dic.Add(Tuple.Create(1, 1), "元旦");
            dic.Add(Tuple.Create(2, 11), "建国記念の日");
            dic.Add(Tuple.Create(2, 14), "バレンタインデー");
            dic.Add(Tuple.Create(3, 3), "ひな祭り");
            dic.Add(Tuple.Create(3, 14), "ホワイトデー");

            for (int i = 1; i < 13; i++) {
                for (int j = 1; j < 32; j++) {
                    string val = null;
                    if (dic.TryGetValue(Tuple.Create(i, j), out val)) {
                        Console.WriteLine(string.Format("{0}月{1}日は{2}", i, j, val));
                    }
                }
            }
        }
    }
2
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4