可以使用字典的 'ContainsValue()' 方法来判断字典中是否存在某个值。

示例代码:

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add('apple', 1);
dict.Add('banana', 2);

if (dict.ContainsValue(1))
{
    Console.WriteLine('字典中存在值为 1 的键值对');
}
else
{
    Console.WriteLine('字典中不存在值为 1 的键值对');
}

输出结果:

字典中存在值为 1 的键值对
C# 字典判断值是否存在 - ContainsValue() 方法

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

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