Linux 拨号vps windows公众号手机端

c#怎么判断字典里是否有某个值

lewis 9年前 (2016-12-10) 阅读数 10 #程序编程
文章标签 c#

在C#中,可以使用ContainsValue方法来判断字典中是否包含某个特定的值。示例代码如下:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary<int, string> dictionary = new Dictionary<int, string>();
        dictionary.Add(1, "apple");
        dictionary.Add(2, "banana");
        dictionary.Add(3, "orange");

        string searchValue = "banana";

        bool containsValue = dictionary.ContainsValue(searchValue);

        if (containsValue)
        {
            Console.WriteLine($"The dictionary contains the value '{searchValue}'.");
        }
        else
        {
            Console.WriteLine($"The dictionary does not contain the value '{searchValue}'.");
        }
    }
}

在上面的示例中,我们首先创建了一个包含键值对的字典,然后使用ContainsValue方法来检查该字典是否包含值为"banana"的条目。最后根据判断结果输出相应的提示信息。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门