c# – 如何按键查找/检查字典值

前端之家收集整理的这篇文章主要介绍了c# – 如何按键查找/检查字典值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图确认特定的字典键是否包含值

例如

dict01在“tester”键中包含短语“testing”

目前我不得不使用KeyPair迭代字典,我不想这样做因为它浪费性能

解决方法

您可以使用 ContainsKeystring.Contains
var key = "tester";
var val = "testing";
if(myDictionary.ContainsKey(key) && myDictionary[key].Contains(val)) 
{
    // "tester" key exists and contains "testing" value
}

你也可以使用TryGetValue

var key = "tester";
var val = "testing";
var dicVal = string.Empty;
if(myDictionary.TryGetValue(key,out dicVal) && dicVal.contains(val)) 
{
    // "tester" key exists and contains "testing" value
}
原文链接:https://www.f2er.com/csharp/96150.html

猜你在找的C#相关文章