C#中的KeyNotFoundException
当您找到的键在Dictionary集合中不可用时,将引发KeyNotFoundException。
让我们看一个例子-
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
try {
var dict = new Dictionary<string, string>() {
{"TV", "Electronics"},
{"Laptop", "Computers"},
};
Console.WriteLine(dict["Pen Drive"]);
}
catch (Exception e) {
Console.WriteLine(e);
}
}
}以下是输出。由于键不在字典中,因此会显示错误KeyNotFoundException错误-
输出结果
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <902ab9e386384bec9c07fa19aa938869>:0
热门推荐
1 薛之谦祝福语简短
10 新年祝福语独创文字简短
11 培训送别祝福语简短的话
12 祝福语年轻回复文案简短
13 爱小姨文案祝福语简短
14 送礼祝福语男性生日简短
15 中班入学祝福语简短大全
16 幸福卡片祝福语英文简短
17 给新年祝福语简短独特
18 给医生简短新年祝福语