C# 辞書Dictionaryの値を更新するサンプル
環境
Windows 10 Home 64bit
Microsoft Visual Studio Community 2022
構文
Dictionary<int, string> 辞書変数名 = new Dictionary<int, string>([キーkey] ="文字列値")
辞書変数名[キー]=更新値
辞書のkeyを指定して値を更新します。
使用例
using System;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dict = new Dictionary<int, string>
{
[0] = "tokyo",
[1] = "oosaka",
[2] = "fukuoka"
};
dict[0] = "TOKYO";
Console.WriteLine(dict[0]);
}
}
}
using System;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dict = new Dictionary<int, string>
{
[0] = "tokyo",
[1] = "oosaka",
[2] = "fukuoka"
};
dict[0] = "TOKYO";
Console.WriteLine(dict[0]);
}
}
}
using System; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string> { [0] = "tokyo", [1] = "oosaka", [2] = "fukuoka" }; dict[0] = "TOKYO"; Console.WriteLine(dict[0]); } } }
実行結果
TOKYO