可以使用 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 的项'。

C# 判断字典中是否存在 Key

原文地址: https://www.cveoy.top/t/topic/oDkD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录