泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱。
很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类:

用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类。我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和拆箱的负担,如果我们操纵的数据类型相对确定的化 用 Dictionary 集合类来存储数据就方便多了,例如我们需要在电子商务网站中存储用户的购物车信息( 商品名,对应的商品个数)时,完全可以用 Dictionary 来存储购物车信息,而不需要任何的类型转化。
下面是简单的例子,包括声明,填充键值对,移除键值对,遍历键值对
Dictionary myDic = new Dictionary();
myDic.Add("aaa", "111");
myDic.Add("bbb", "222");
myDic.Add("ccc", "333");
myDic.Add("ddd", "444");
//如果添加已经存在的键,add方法会抛出异常
try
{
myDic.Add("ddd","ddd");
}
catch (ArgumentException ex)
{
Console.WriteLine("此键已经存在:" + ex.Message);
}
//解决add()异常的方法是用ContainsKey()方法来判断键是否存在
if (!myDic.ContainsKey("ddd"))
{
myDic.Add("ddd", "ddd");
}
else
{
Console.WriteLine("此键已经存在:");
}
//而使用索引器来负值时,如果建已经存在,就会修改已有的键的键值,而不会抛出异常
myDic ["ddd"]="ddd";
myDic["eee"] = "555";
//使用索引器来取值时,如果键不存在就会引发异常
try
{
Console.WriteLine("不存在的键""fff""的键值为:" + myDic["fff"]);
}
catch (KeyNotFoundException ex)
{
Console.WriteLine("没有找到键引发异常:" + ex.Message);
}
//解决上面的异常的方法是使用ContarnsKey() 来判断时候存在键,如果经常要取健值得化最好用 TryGetValue方法来获取集合中的对应键值
string value = "";
if (myDic.TryGetValue("fff", out value))
{
Console.WriteLine("不存在的键""fff""的键值为:" + value );
}
else
{
Console.WriteLine("没有找到对应键的键值");
}
//下面用foreach 来遍历键值对
//泛型结构体 用来存储健值对
foreach (KeyValuePair kvp in myDic)
{
Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
}
//获取值得集合
foreach (string s in myDic.Values)
{
Console.WriteLine("value={0}", s);
}
//获取值得另一种方式
Dictionary.ValueCollection values = myDic.Values;
foreach (string s in values)
{
Console.WriteLine("value={0}", s);
}
常用的属性和方法如下:

例如:
List listDict = new List();
Dictionary dict = new Dictionary();
dict.Add("key1","value");
dict.Add("key2","value");
listDict.Add(dict);
Dictionary dict1 = new Dictionary();
dict1.Add("key1","value");
dict1.Add("key2","value");
listDict.Add(dict1);
爱词霸英语为广大英语学习爱好者提供金山词霸、在线词典、在线翻译、英语学习资料、英语歌曲、英语真题在线测试、汉语查词等服务,爱词霸英语在线查词和在线翻译频道致力于为您提供优质的在线查词及在 …该网站已通过中网可信网站验证
2 天前 · 中文字典(简) / Chinese Dictionary 12,589,558,014 访问者服务 搜索 / 页面工具 TheFreeDictionary Google? Keyboard A A A A 种语言 Share on Facebook Twitter 获取我们 的应用 註冊 登入 登入 / 註冊 Facebook Twitter Google ...https://zh.thefreedictionary.com
Dictionary definition is - a reference source in print or electronic form containing words usually alphabetically arranged along with information about their forms, pronunciations, functions, etymologies, meanings, and syntactic and idiomatic uses. How to use dictionary in a sentence.https://www.merriam-webster.com/dictionary/dictionary
2 天前 · 在上面的搜索框内输入条件,点击检索,就可以找到相应汉字的拼音、部首、笔划、注解、出处,也可以通过笔划、部首去检索。一些淘汰不用、电脑输不出的汉字,请通过在线康熙字典查找,给孩子起名,请通过在线起名大全进行起名字。xh.5156edu.com
2 天前 · Trusted free online English Dictionary from Collins. Authoritative, reliable and up-to-date content for English word reference, with images, example sentences, audio and …https://www.collinsdictionary.com/dictionary
Define dictionary. dictionary synonyms, dictionary pronunciation, dictionary translation, English dictionary definition of dictionary. n. pl. dic·tion·ar·ies 1.https://www.thefreedictionary.com/dictionary
Longman English Dictionary - the leading dictionary for learners of English of all levels: definitions, idioms, examples and more.https://www.ldoceonline.com
2021-1-16 · Collins is a major publisher of Educational, Language and Geographic content, and have been publishing innovative, inspiring and informative books for over 200 years. Collins online dictionary and reference resources draw on the wealth of reliable and authoritative information about language, thanks to the extensive use of our corpora - vast databases of language - both in English and in other ...https://www.collinsdictionary.com
The search engine that helps you find exactly what you're looking for. Find the most relevant information, video, images, and answers from all across the Web. US leaders boost security at state capitols across the nation amid... CNN 54 分鐘前 The FBI has warned ...https://hk.dictionary.search.yahoo.com