hashtabel与dictionary的区别-创新互联
1 :
本文名称:hashtabel与dictionary的区别-创新互联
网站网址:http://azwzsj.com/article/dcjhdp.html
Hashtable
支持多个线程读, 同时只有一个线程写 , dictionary 不支持
Hashtable
supports multiple reader threads with a single writer thread, while Dictionary
offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) use ConcurrentDictionary
2 : hash table 只能用 key 引用, 而不能 foreach , dictionary 可以。
引用 hash table:
public void MethodHashTable()
{
Hashtable objHashTable= new Hashtable();
objHashTable.Add(1, 100); // int objHashTable.Add(2.99, 200); // float objHashTable.Add('A', 300);// char objHashTable.Add("4", 400);// string
lblDisplay1.Text= objHashTable[1].ToString();
lblDisplay2.Text= objHashTable[2.99].ToString();
lblDisplay3.Text= objHashTable['A'].ToString();
lblDisplay4.Text= objHashTable["4"].ToString();
// ----------- Not Possible for HashTable ----------
//foreach (KeyValuePair pair in objHashTable)
//{
// lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
//}}
引用 dictionary :
public void MethodDictionary()
{
Dictionary dictionary = new Dictionary();
dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);
//dictionary.Add(1, -2);// Compilation Error foreach (KeyValuePair pair in dictionary)
{
lblDisplay.Text= pair.Value + " " + lblDisplay.Text;
}
}
本文名称:hashtabel与dictionary的区别-创新互联
网站网址:http://azwzsj.com/article/dcjhdp.html