Skip to content

Instantly share code, notes, and snippets.

@todorok1
Last active November 3, 2020 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/5842946bb98396ae08061853451364e3 to your computer and use it in GitHub Desktop.
Save todorok1/5842946bb98396ae08061853451364e3 to your computer and use it in GitHub Desktop.
C#のDictionaryでキーの有無を調べるサンプル
// Addメソッドで値を追加します。
itemDict.Add(1, "ポーション");
itemDict.Add(2, "ハイポーション");
itemDict.Add(3, "エーテル");
// キーがなければ追加します。
if (!itemDict.ContainsKey(3))
{
itemDict.Add(3, "エクスポーション");
}
// キーがなければ追加します。
if (!itemDict.ContainsKey(4))
{
itemDict.Add(4, "エリクサー");
}
foreach (KeyValuePair<int, string> pair in itemDict)
{
int itemId = pair.Key;
string itemName = pair.Value;
Debug.Log(itemId + ": " + itemName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment