Hashtableが同期されているかどうかを確認しますC#
ハッシュテーブルが同期されているかどうかを確認するためのコードは次のとおりです-
例
using System;
using System.Collections;
public class Demo {
public static void Main() {
Hashtable hash = new Hashtable();
hash.Add("One", "Katie");
hash.Add("Two", "John");
hash.Add("Three", "Barry");
hash.Add("Four", "");
hash.Add("Five","Harry");
hash.Add("Six", "F");
hash.Add("Seven", "Tom");
hash.Add("Eight","Andy");
hash.Add("Nine", "I");
hash.Add("Ten", "Tim");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash) {
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Is the Hashtable having fixed size? = "+hash.IsFixedSize);
Console.WriteLine("If Hashtable read-only? = "+hash.IsReadOnly);
Hashtable hash2 = Hashtable.Synchronized(hash);
Console.WriteLine("Is Hash synchronized = "+hash2.IsSynchronized);
}
} 出力
これにより、次の出力が生成されます-
Hashtable Key and Value pairs... One and Katie Ten and Tim Five and Harry Three and Barry Seven and Tom Two and John Four and Eight and Andy Nine and I Six and F Is the Hashtable having fixed size? = False If Hashtable read-only? = False Is Hash synchronized = True
例
別の例を見てみましょう-
using System;
using System.Collections;
public class Demo {
public static void Main() {
Hashtable hash = new Hashtable();
hash.Add("One", 1);
hash.Add("Two", 2);
hash.Add("Three", 2);
hash.Add("Four", 4);
hash.Add("Five",5);
hash.Add("Six", 6);
hash.Add("Seven", 7);
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash) {
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Is the Hashtable having fixed size? = "+hash.IsFixedSize);
Console.WriteLine("If Hashtable read-only? = "+hash.IsReadOnly);
Console.WriteLine("Is Hash synchronized = "+hash.IsSynchronized);
}
} 出力
これにより、次の出力が生成されます-
Hashtable Key and Value pairs... One and 1 Five and 5 Three and 2 Seven and 7 Two and 2 Four and 4 Six and 6 Is the Hashtable having fixed size? = False If Hashtable read-only? = False Is Hash synchronized = False
-
C#でハッシュテーブルをクリアする
C#のClear()メソッドを使用して、ハッシュテーブルをクリアします。 以下は私たちのハッシュテーブルです- Hashtable h = new Hashtable(); h.Add(1, "Amit"); h.Add(2, "Sachin"); h.Add(3, "Rahul"); 明確な方法を使用してください。 h.Clear(); ここでハッシュテーブルを表示しようとすると、ハッシュテーブルが空であるため、何も表示されません。 例 using System; using System.Collections; publi
-
C#でのハッシュテーブルと辞書
ハッシュテーブル ハッシュテーブルは、キーを使用して要素にアクセスする必要がある場合に使用され、有用なキー値を識別できます。ハッシュテーブルの各アイテムには、キーと値のペアがあります。キーは、コレクション内のアイテムにアクセスするために使用されます。 ハッシュテーブルのメンバーはスレッドセーフです。存在しないキーを見つけようとすると、nullを返します。ハッシュテーブルは汎用タイプではありません。 ハッシュテーブルコレクションは、ボクシングとアンボクシングが必要なため、辞書よりも低速です。 ハッシュテーブルを宣言するには- Hashtable ht = new Hashtable();