Code Fix

中級The given key was not present in the dictionary の原因と直し方 › パターン1

The given key was not present in the dictionary の原因と直し方

Dictionaryに登録されていないキーをインデクサで読み取ろうとすると発生します。

 1  class Program {
 2      static void Main(string[] args) {
 3          var scores = new System.Collections.Generic.Dictionary<string, int>();
 4          scores["alice"] = 90;
 5          System.Console.WriteLine(scores[_______]);
                                               ^
 6      }
 7  }
Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'bob' was not present in the dictionary. at Program.Main(String[] args) in Program.cs:line 5

空欄を埋めてビルドを通す

広告
広告スロット(未設定)

The given key was not present in the dictionary の原因と直し方

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)