如何检查一个键/值对是否存在于Dictionary<TKey,TValue>
中? 我可以使用ContainsKey
和ContainsValue
来检查是否存在键或值,但我不确定如何检查是否存在键/值对。
如何检查一个键/值对是否存在于Dictionary<TKey,TValue>
中? 我可以使用ContainsKey
和ContainsValue
来检查是否存在键或值,但我不确定如何检查是否存在键/值对。
如果 key 不存在,则无法存在 pair... 因此获取与该 key 相关联的值,并检查它是否是您要查找的值。例如:
// Could be generic of course, but let's keep things simple...
public bool ContainsKeyValue(Dictionary<string, int> dictionary,
string expectedKey, int expectedValue)
{
int actualValue;
if (!dictionary.TryGetValue(expectedKey, out actualValue))
{
return false;
}
return actualValue == expectedValue;
}
或者稍微更"聪明"一些(通常要避免这样做):
public bool ContainsKeyValue(Dictionary<string, int> dictionary,
string expectedKey, int expectedValue)
{
int actualValue;
return dictionary.TryGetValue(expectedKey, out actualValue) &&
actualValue == expectedValue;
}
一个字典只支持每个键对应一个值,因此:
// key = the key you are looking for
// value = the value you are looking for
YourValueType found;
if(dictionary.TryGetValue(key, out found) && found == value) {
// key/value pair exists
}
if (myDic.ContainsKey(testKey) && myDic[testKey].Equals(testValue))
return true;
Dictionary<string, bool> clients = new Dictionary<string, bool>();
clients.Add("abc", true);
clients.Add("abc2", true);
clients.Add("abc3", false);
clients.Add("abc4", true);
bool isValid = false;
if (clients.TryGetValue("abc3", out isValid)==false||isValid == false)
{
Console.WriteLine(isValid);
}
else
{
Console.WriteLine(isValid);
}
First Section{clients.TryGetValue("abc3", out isValid)==false}||Second Section{isValid == false}
简单通用的扩展方法
这是一个快速的通用扩展方法,可以为任何IDictionary
添加一个ContainsPair()
方法:
public static bool ContainsPair<TKey, TValue>(this IDictionary<TKey, TValue> dictionary,
TKey key,
TValue value)
{
return dictionary.TryGetValue(key, out var found) && found.Equals(value);
}
这使您可以执行以下类似于字典的检查:
if( myDict.ContainsPair("car", "mustang") ) { ... } // NOTE: this is not case-insensitive
不区分大小写的检查
在使用基于字符串的键时,可以通过在构建字典时使用比较器(例如StringComparer.OrdinalIgnoreCase
)来使Dictionary
在其键方面不区分大小写。
然而,要使值比较不区分大小写(假设值也是字符串),则可以使用此版本,它添加了一个IComparer
参数:
public static bool ContainsPair<TKey, TValue>(this IDictionary<TKey, TValue> dictionary,
TKey key,
TValue value,
IComparer<TValue> comparer)
{
return dictionary.TryGetValue(key, out var found) && comparer.Compare(found, value) == 0;
}
Jon Skeet的答案的通用版本
public bool ContainsKeyValue<TKey, TVal>(Dictionary<TKey, TVal> dictionnaire,
TKey expectedKey,
TVal expectedValue) where TVal : IComparable
{
TVal actualValue;
if (!dictionnaire.TryGetValue(expectedKey, out actualValue))
{
return false;
}
return actualValue.CompareTo(expectedValue) == 0;
}
类似这样的东西怎么实现
bool exists = dict.ContainsKey("key") ? dict["key"] == "value" : false;
这个功能已经可以直接使用,但是它被隐藏在ICollection<KeyValuePair<TKey, TValue>>
接口的Contains
方法中,而这个接口被Dictionary<TKey,TValue>
类实现。下面是如何将其作为public
扩展方法公开:
public static bool Contains<TKey, TValue>(this Dictionary<TKey, TValue> dictionary,
TKey key, TValue value)
{
ArgumentNullException.ThrowIfNull(dictionary);
return ((ICollection<KeyValuePair<TKey, TValue>>)dictionary).Contains(new(key, value));
}
在线演示。
var result= YourDictionaryName.TryGetValue(key, out string value) ? YourDictionaryName[key] : "";
首先,您需要检查键是否存在,如果存在,则获取该键的值并将其与您正在测试的值进行比较... 如果它们相等,则您的字典包含该键值对