c# 判断字典中是否存在key
可以使用Dictionary类的ContainsKey方法来判断字典中是否存在指定的key。示例代码如下:
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 1);
dict.Add("banana", 2);
if (dict.ContainsKey("apple"))
{
Console.WriteLine("字典中存在key为apple的项");
}
else
{
Console.WriteLine("字典中不存在key为apple的项");
}
输出结果为:“字典中存在key为apple的项”。
原文地址: https://www.cveoy.top/t/topic/halI 著作权归作者所有。请勿转载和采集!