自己做报名网站教程,国外做贸易网站,大型机械网站建设公司,昆明关键词优化软件总目录 C# 语法总目录 集合五 Dictionary 1. Dictionary 1. Dictionary
字典是键值对集合#xff0c;通过键值对来查找 Dictionary和Hashtable的区别是Dictionary可以用泛型#xff0c;而HashTable不能用泛型 OrderedDictionary 是按照添加元素时的顺序的字典#xff0c;是… 总目录 C# 语法总目录 集合五 Dictionary 1. Dictionary 1. Dictionary
字典是键值对集合通过键值对来查找 Dictionary和Hashtable的区别是Dictionary可以用泛型而HashTable不能用泛型 OrderedDictionary 是按照添加元素时的顺序的字典是一个非泛型字典可以用索引访问文员也可以用键来访问元素类似HashTable和ArrayList的结合
其他类型 ListDictionary是使用一个独立链表来存储实际的数据数据多时效率不好 HybridDictionary是为了解决ListDictionary数据多时效率不好的情况用来替代ListDictionary。 SortedDictionary内部由红黑树实现内部根据键进行排序
//Hashtable
Hashtable hashtable new Hashtable();
hashtable.Add(name, lisi);
hashtable.Add(txt, notepad.exe);
hashtable.Add(bmp, paint.exe);
hashtable.Add(dib, paint.exe);
hashtable.Add(rtf, wordpad.exe);
try
{hashtable.Add(txt, winword.exe);
}
catch (Exception)
{Console.WriteLine(An element with Key \txt\ already exists.);
}Console.WriteLine(For key \txt\, value {0}., hashtable[txt]);//An element with Key txt already exists.
//For key txt, value notepad.exe.foreach (DictionaryEntry de in hashtable)
{Console.WriteLine(Key {0}, Value {1}, de.Key, de.Value);
}//Key dib, Value paint.exe
//Key bmp, Value paint.exe
//Key rtf, Value wordpad.exe
//Key name, Value lisi
//Key txt, Value notepad.exeif (!hashtable.ContainsKey(doc))
{Console.WriteLine(Key \doc\ is not found.);
}//Dictionary
Dictionarystring, int dics new Dictionarystring, int();dics.Add(name, 123);
dics.TryAdd(name, 456);//如果有了键那么就不会再添加了dics.TryAdd(age, 50);
dics[age] 45; //修改
dics.Remove(age);
dics.Remove(time);//不管有没有也不报错Console.WriteLine(dics[name]);int value;
Console.WriteLine(dics.TryGetValue(name,out value));Console.WriteLine(value);
//123
//True
//123总目录 C# 语法总目录