中級 › The given key was not present in the dictionary の原因と直し方 › パターン3
The given key was not present in the dictionary の原因と直し方
Dictionaryに登録されていないキーをインデクサで読み取ろうとすると発生します。
1 class Program { 2 static void Main(string[] args) { 3 var prices = new System.Collections.Generic.Dictionary<string, int>(); 4 prices["apple"] = 100; 5 System.Console.WriteLine(prices[________]); ^ 6 } 7 }
Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'banana' was not present in the dictionary.
at Program.Main(String[] args) in Program.cs:line 5
空欄を埋めてビルドを通す
大文字小文字も区別されます。"apple"と"Apple"は別のキーとして扱われます。
次の問題