C#のDictionary.ContainsValue()メソッドの使い方を解説
C#のDictionary.ContainsValue()メソッドは、Dictionary<TKey, TValue>コレクション内に指定した値が存在するかどうかを確認するためのメソッドです。値が見つかった場合は true を返し、見つからない場合は false を返します。
構文
public bool ContainsValue (TValue val);
パラメータ val には、検索対象となる値を指定します。
ContainsValue()メソッドの特徴
- 線形探索(O(n))で値を検索するため、要素数が多い場合は処理に時間がかかることがあります。
- キーの検索に使用する
ContainsKey()メソッド(O(1))と比べて処理速度が遅い点に注意が必要です。 - 値の比較には、既定の等値比較子(EqualityComparer<TValue>.Default)が使用されます。
サンプルコード1:値が存在する場合
それでは、Dictionary.ContainsValue()メソッドを実装した例を見てみましょう。
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = "+dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Kevin"))
Console.WriteLine("Value found!");
else
Console.WriteLine("Value isn't in the dictionary!");
dict.Clear();
Console.WriteLine("Cleared Key/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.WriteLine("Count of elements now = "+dict.Count);
}
}出力結果
上記のコードを実行すると、次のような出力が得られます。
Count of elements = 5 Key/value pairs... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan Value found! Cleared Key/value pairs... Count of elements now = 0
この例では、辞書内に「Kevin」という値が存在するため、「Value found!」と表示されています。その後、Clear()メソッドですべての要素を削除し、要素数が0になっていることを確認できます。
サンプルコード2:値が存在しない場合
続いて、検索対象の値が辞書に存在しない場合の例を見てみましょう。
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "Chris");
dict.Add("Two", "Steve");
dict.Add("Three", "Messi");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = "+dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Angelina"))
Console.WriteLine("Value found!");
else
Console.WriteLine("Value isn't in the dictionary!");
dict.Clear();
Console.WriteLine("Cleared Key/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.WriteLine("Count of elements now = "+dict.Count);
}
}出力結果
上記のコードを実行すると、次のような出力が得られます。
Count of elements = 5 Key/value pairs... Key = One, Value = Chris Key = Two, Value = Steve Key = Three, Value = Messi Key = Four, Value = Ryan Key = Five, Value = Nathan Value isn't in the dictionary! Cleared Key/value pairs... Count of elements now = 0
この例では、「Angelina」という値は辞書内に存在しないため、「Value isn't in the dictionary!」と表示されました。
まとめ
Dictionary.ContainsValue()メソッドを使用することで、C#のDictionaryコレクション内に特定の値が存在するかどうかを簡単に判定できます。ただし、キー検索のContainsKey()メソッドと異なり線形探索が行われるため、パフォーマンスが重要な場面では注意して使用しましょう。
-
C#のChar.IsSymbol()メソッドとは?文字が記号かどうかを判定する方法を解説
Char.IsSymbol()メソッドとはC#のChar.IsSymbol()メソッドは、指定された文字列内の指定位置にある文字が、Unicodeの「記号(Symbol)」カテゴリに分類されているかどうかを判定するメソッドです。戻り値はbool型で、記号であればtrue、そうでなければfalseを返します。判定対象となるのは、数学記号(+、=など)、通貨記号($、¥など)、修飾記号、その他の記号といったUnicode記号カテゴリに属する文字です。構文public static bool IsSymbol (string str, int index);パラメータstr:評価対象の文字列inde
-
C#のDictionary.ContainsKey()メソッドの使い方を徹底解説!サンプルコード付き
C#のDictionary.ContainsKey()メソッドは、Dictionary<TKey, TValue>内に指定したキーが存在するかどうかを判定するためのメソッドです。キーが見つかればtrue、見つからなければfalseを返します。構文public bool ContainsKey (TKey key);パラメータ「key」には、検索対象となるキーを指定します。使用例1:キーが存在する場合using System; using System.Collections.Generic; public class Demo { public static void Ma