温馨提示×

C# IDictionary中contains方法

c#
小樊
88
2024-07-13 13:05:22
栏目: 编程语言

在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"是否存在于字典中。如果存在,则打印出相应的提示信息。

0