SortedDictionaryに指定されたキーが含まれているかどうかをC#で確認します
SortedDictionaryに指定されたキーが含まれているかどうかを確認するには、コードは次のとおりです-
例
using System; using System.Collections; using System.Collections.Generic; public class Demo { public static void Main() { SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>(); sortedDict.Add(100, "Mobile"); sortedDict.Add(200, "Laptop"); sortedDict.Add(300, "Desktop"); sortedDict.Add(400, "Speakers"); sortedDict.Add(500, "Headphone"); sortedDict.Add(600, "Earphone"); Console.WriteLine("SortedDictionary key-value pairs..."); IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator(); while (demoEnum.MoveNext()) Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value); Console.WriteLine("\nThe SortedDictionary has the key 200? = "+sortedDict.ContainsKey(200)); } }
出力
これにより、次の出力が生成されます-
SortedDictionary key-value pairs... Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone The SortedDictionary has the key 200? = True
例
別の例を見てみましょう-
using System; using System.Collections; using System.Collections.Generic; public class Demo { public static void Main() { SortedDictionary<string, string> sortedDict = new SortedDictionary<string, string>(); sortedDict.Add("A", "John"); sortedDict.Add("B", "Andy"); sortedDict.Add("C", "Tim"); sortedDict.Add("D", "Ryan"); sortedDict.Add("E", "Kevin"); sortedDict.Add("F", "Katie"); sortedDict.Add("G", "Brad"); Console.WriteLine("SortedDictionary key-value pairs..."); IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator(); while (demoEnum.MoveNext()) Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value); Console.WriteLine("\nThe SortedDictionary has the key F? = "+sortedDict.ContainsKey("F")); } }
出力
これにより、次の出力が生成されます-
SortedDictionary key-value pairs... Key = A, Value = John Key = B, Value = Andy Key = C, Value = Tim Key = D, Value = Ryan Key = E, Value = Kevin Key = F, Value = Katie Key = G, Value = Brad The SortedDictionary has the key F? = True
-
HashSetにC#で指定された要素が含まれているかどうかを確認します
HashSetに指定された要素が含まれているかどうかを確認するためのコードは、次のとおりです- 例 using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<int> set1 = new HashSet<int>(); set1.Add(25); set1.Add(50);
-
配列にC#で指定された条件に一致する要素が含まれているかどうかを確認します
配列に特定の条件に一致する要素が含まれているかどうかを確認するには、C#でStartsWith()メソッドを使用できます- 例 using System; public class Demo { public static void Main(){ string[] products = { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "F