C#でListDictionaryへの同期アクセスを取得するにはどうすればよいですか?
ListDictionaryへの同期アクセスを取得するには、コードは次のとおりです-
例
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { ListDictionary dict = new ListDictionary(); dict.Add("1", "SUV"); dict.Add("2", "Sedan"); dict.Add("3", "Utility Vehicle"); dict.Add("4", "Compact Car"); dict.Add("5", "SUV"); dict.Add("6", "Sedan"); dict.Add("7", "Utility Vehicle"); dict.Add("8", "Compact Car"); dict.Add("9", "Crossover"); dict.Add("10", "Electric Car"); Console.WriteLine("ListDictionary elements..."); foreach(DictionaryEntry d in dict) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("\nIs the ListDictionary having fixed size? = "+dict.IsFixedSize); Console.WriteLine("If ListDictionary read-only? = "+dict.IsReadOnly); Console.WriteLine("Is ListDictionary synchronized = "+dict.IsSynchronized); Console.WriteLine("The ListDictionary has the key K? = "+dict.Contains("K")); Console.WriteLine("The ListDictionary has the key 9? = "+dict.Contains("9")); Console.WriteLine("\nSynchronize access..."); lock(dict.SyncRoot) { foreach(DictionaryEntry d in dict) { Console.WriteLine(d.Key + " " + d.Value); } } } }
出力
これにより、次の出力が生成されます-
ListDictionary elements... 1 SUV 2 Sedan 3 Utility Vehicle 4 Compact Car 5 SUV 6 Sedan 7 Utility Vehicle 8 Compact Car 9 Crossover 10 Electric Car Is the ListDictionary having fixed size? = False If ListDictionary read-only? = False Is ListDictionary synchronized = False The ListDictionary has the key K? = False The ListDictionary has the key 9? = True Synchronize access... 1 SUV 2 Sedan 3 Utility Vehicle 4 Compact Car 5 SUV 6 Sedan 7 Utility Vehicle 8 Compact Car 9 Crossover 10 Electric Car
例
別の例を見てみましょう-
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { ListDictionary dict = new ListDictionary(); dict.Add("A", "Books"); dict.Add("B", "Electronics"); dict.Add("C", "Appliances"); dict.Add("D", "Pet Supplies"); dict.Add("E", "Clothing"); dict.Add("F", "Footwear"); Console.WriteLine("Synchronize access..."); lock(dict.SyncRoot) { foreach(DictionaryEntry d in dict) { Console.WriteLine(d.Key + " " + d.Value); } } } }
出力
これにより、次の出力が生成されます-
Synchronize access... A Books B Electronics C Appliances D Pet Supplies E Clothing F Footwear
-
C#でタプルの3番目の要素を取得するにはどうすればよいですか?
タプルの3番目の要素を取得するためのコードは、次のとおりです- 例 using System; public class Demo { public static void Main(String[] args){ var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500); var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500); &nbs
-
C#でタプルの6番目の要素を取得するにはどうすればよいですか?
タプルの6番目の要素を取得するためのコードは、次のとおりです- 例 using System; public class Demo { public static void Main(String[] args){ var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500); var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500); &nbs