「C#入門」Dictionaryの要素のキーと値をセットするサンプル

説明
Dictionary<TKey,TValue> クラス
TKey
ディクショナリ内のキーの型。
TValue
ディクショナリ内の値の型。

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Collections.Generic;
class DemoA
{
static void Main()
{
var citys = new Dictionary<string, string>()
{ {"key1","東京"},
{"key2","大阪"},
{"key3","横浜"},
{"key4","福島"},
};
foreach (var cft in citys)
{
Console.WriteLine(cft.Key);
Console.WriteLine(cft.Value);
}
}
}
using System; using System.Collections.Generic; class DemoA { static void Main() { var citys = new Dictionary<string, string>() { {"key1","東京"}, {"key2","大阪"}, {"key3","横浜"}, {"key4","福島"}, }; foreach (var cft in citys) { Console.WriteLine(cft.Key); Console.WriteLine(cft.Value); } } }
using System;
using System.Collections.Generic;

class DemoA
{
    static void Main()
    {
        var citys = new Dictionary<string, string>()
        { {"key1","東京"},
          {"key2","大阪"},
          {"key3","横浜"},
          {"key4","福島"},
        };

        foreach (var cft in citys)
        {
            Console.WriteLine(cft.Key);
            Console.WriteLine(cft.Value);
        }
    }
}

結果
key1 key2 key3 key4
東京 大阪 横浜 福島

C#

Posted by arkgame