温馨提示×

C# IDictionary中contains方法

c#
小樊
97
2024-07-13 13:05:22
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C#中,IDictionary接口并没有提供contains方法。要检查指定的键是否存在于IDictionary中,可以使用ContainsKey方法。示例如下:

IDictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("key1", "value1");
dictionary.Add("key2", "value2");

if (dictionary.ContainsKey("key1"))
{
    Console.WriteLine("Key1 exists in the dictionary.");
}
else
{
    Console.WriteLine("Key1 does not exist in the dictionary.");
}

在上面的示例中,我们通过ContainsKey方法检查了键"key1"是否存在于字典中。如果存在,则打印出相应的提示信息。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c# dynamicobject怎样提高效率

0