「C#」ContainsValueでDictionaryの特定の値があるか真偽値を返す

2021年10月8日

構文
public bool ContainsValue (TValue value);
指定した値を持つ要素が true に格納されている場合は Dictionary<TKey,TValue>。
それ以外の場合は false。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Collections.Generic;
class Arkgame
{
public static void Main()
{
//Dictionaryクラスをインスタンス
var citys = new Dictionary<int, string>();
citys.Add(101, "東京");
citys.Add(202, "大阪");
citys.Add(303, "福岡");
var str = "大阪";
//指定の値があるか真偽値を返す
if (citys.ContainsValue(str))
{
Console.WriteLine("指定の値「" + str + "」が存在します");
}
Console.ReadKey();
}
}
using System; using System.Collections.Generic; class Arkgame { public static void Main() { //Dictionaryクラスをインスタンス var citys = new Dictionary<int, string>(); citys.Add(101, "東京"); citys.Add(202, "大阪"); citys.Add(303, "福岡"); var str = "大阪"; //指定の値があるか真偽値を返す if (citys.ContainsValue(str)) { Console.WriteLine("指定の値「" + str + "」が存在します"); } Console.ReadKey(); } }
using System;
using System.Collections.Generic;
class Arkgame
{
    public static void Main()
    {
        //Dictionaryクラスをインスタンス
        var citys = new Dictionary<int, string>();
        citys.Add(101, "東京");
        citys.Add(202, "大阪");
        citys.Add(303, "福岡");

        var str = "大阪";
        //指定の値があるか真偽値を返す
        if (citys.ContainsValue(str))
        {
            Console.WriteLine("指定の値「" + str + "」が存在します");
        }

        Console.ReadKey();
    }
}

実行結果
指定の値「大阪」が存在します

C#

Posted by arkgame