检查哈希表是否已同步C#
要检查Hashtable是否同步,代码如下-
示例
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
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短