C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#でStringDictionaryが同期されているかどうかを確認する方法

C#の StringDictionary が同期(スレッドセーフ)されているかどうかを確認するには、IsSynchronized プロパティを使用します。このプロパティは、コレクションへのアクセスが同期されている場合は true、同期されていない場合は false を返します。

通常の StringDictionary インスタンスは既定で同期されていないため、多くの場合 False が出力されます。それでは、具体的なサンプルコードを見ていきましょう。

例1:IsSynchronizedプロパティの基本的な使い方

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      Console.WriteLine("StringDictionary1 elements...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("1", "A");
      strDict2.Add("2", "B");
      strDict2.Add("3", "C");
      strDict2.Add("4", "D");
      strDict2.Add("5", "E");
      Console.WriteLine("\nStringDictionary2 key-value pairs...");
      IEnumerator demoEnum = strDict2.GetEnumerator();
      DictionaryEntry d;
      while (demoEnum.MoveNext()) {
         d = (DictionaryEntry)demoEnum.Current;
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("Is the StringDictionary2 synchronized? = "+strDict2.IsSynchronized);
   }
}

出力結果

上記のコードを実行すると、以下のような出力が得られます。

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E
Is the StringDictionary2 synchronized? = False

例2:別のサンプルコードでの確認

続いて、もう一つの例を見てみましょう。ここでは、foreachループではなく IEnumerator を使ってキーと値のペアを列挙しています。

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict = new StringDictionary();
      strDict.Add("One", "Laptop");
      strDict.Add("Two", "Desktop");
      strDict.Add("Three", "Earphone");
      strDict.Add("Four", "Speaker");
      strDict.Add("Five", "HardDisk");
      strDict.Add("Six", "SSD");
      strDict.Add("Seven", "HardDisk");
      Console.WriteLine("StringDictionary key-value pairs...");
      IEnumerator demoEnum = strDict.GetEnumerator();
      DictionaryEntry d;
      while (demoEnum.MoveNext()) {
         d = (DictionaryEntry)demoEnum.Current;
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("Is StringDictionary synchronized? = "+strDict.IsSynchronized);
   }
}

出力結果

このコードを実行すると、次の出力が表示されます。

StringDictionary key-value pairs...
Key = five, Value = HardDisk
Key = one, Value = Laptop
Key = four, Value = Speaker
Key = three, Value = Earphone
Key = seven, Value = HardDisk
Key = six, Value = SSD
Key = two, Value = Desktop
Is StringDictionary synchronized? = False

ポイントまとめ

  • IsSynchronized プロパティは、StringDictionary がスレッドセーフかどうかを示す真偽値を返します。
  • StringDictionary のキーは内部的に小文字に変換されるため、出力では「One」が「one」として表示されます。
  • マルチスレッド環境で安全にアクセスしたい場合は、Synchronized() メソッドでラップした同期済みラッパーを作成する必要があります。
  1. C#のCollectionに要素が含まれているかどうかを確認する方法

    C#でコレクション(Collection)内に特定の要素が存在するかどうかを確認するには、Contains()メソッドを使用します。このメソッドは、指定した要素がコレクション内に見つかった場合は true、見つからなかった場合は false を返します。以下に具体的なコード例を示します。例1:整数のコレクションの場合using System; using System.Collections.ObjectModel; public class Demo {     public static void Main(){    

  2. C#でリストが空かどうかを確認する方法【Anyメソッドの使い方】

    C#でリストが空かどうかを確認するには?C#でList<T>が空かどうかを確認するには、LINQのAnyメソッドを使用するのが一般的です。Anyメソッドは、シーケンスに要素が1つでも含まれていればtrueを返し、空の場合はfalseを返します。この結果を否定演算子!で反転させることで、リストが空かどうかを簡単に判定できます。リストの作成まず、確認対象となるリストを作成しましょう。var subjects = new List<string>(); subjects.Add("Maths"); subjects.Add("Java"