「C#」keysプロパティでDictionaryのキーの集合を取得する

2021年10月8日

書式
foreach (var 変数名 in Dictionaryのインスタンス名.Keys)
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Collections.Generic;
class Arkgame
{
public static void Main()
{
//Dictionaryクラスをインスタンス
var citys = new Dictionary<int, string>();
citys.Add(101, "東京");
citys.Add(202, "大阪");
citys.Add(303, "福岡");
//keysプロパティでキーの集合を取得
foreach (var cc in citys.Keys)
{
Console.WriteLine("キー: " + cc);
}
Console.ReadKey();
}
}
using System; using System.Collections.Generic; class Arkgame { public static void Main() { //Dictionaryクラスをインスタンス var citys = new Dictionary<int, string>(); citys.Add(101, "東京"); citys.Add(202, "大阪"); citys.Add(303, "福岡"); //keysプロパティでキーの集合を取得 foreach (var cc in citys.Keys) { Console.WriteLine("キー: " + cc); } Console.ReadKey(); } }
using System;
using System.Collections.Generic;
class Arkgame
{
    public static void Main()
    {
        //Dictionaryクラスをインスタンス
        var citys = new Dictionary<int, string>();
        citys.Add(101, "東京");
        citys.Add(202, "大阪");
        citys.Add(303, "福岡");

        //keysプロパティでキーの集合を取得
        foreach (var cc in citys.Keys)
        {
            Console.WriteLine("キー: " + cc);
        }

        Console.ReadKey();
    }
}

実行結果
キー: 101
キー: 202
キー: 303

C#

Posted by arkgame