理解C#泛型集合—Dictionary<K,V>

来源:互联网 发布:手机淘宝怎么改支付宝 编辑:程序博客网 时间:2024/05/16 08:51

1、要使用Dictionary集合,需要导入C#泛型命名空间  

  System.Collections.Generic(程序集:mscorlib)  

2、描述  

   1)、从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成  

   2)、任何键都必须是唯一的  

   3)、键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值  

   4)、Key和Value可以是任何类型(stringint,custom class 等)  

3、创建及初始化  

   Dictionary<intstring> myDictionary = new Dictionary<intstring>();  

  

4、添加元素  ,使用Dictionary<K,V>.Add(key,value) 方法。

   myDictionary.Add("C#",0);  

   myDictionary.Add("C++",1);  

  

5、查找元素By Key, 使用Dictionary<K,V>.ContainsKey(key)方法,返回Bool值,判断字典中是否存在此键。

  

  if(myDictionary.ContainsKey("C#"))  

  {  

    Console.WriteLine("Key:{0},Value:{1}""C#", myDictionary["C#"]);  

  }  

  

6.遍历元素 By KeyValuePair ,直接遍历字典,使用System.Collection.Generic.KeyValuePair 类 的对象来遍历字典的键值对。

  

  foreach (KeyValuePair<stringint> kvp in myDictionary)  

  {  

    Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value);  

  }  

遍历键值对By Enumerator, 使用Dictionary<K,V>.GetEnumerator() 方法, 返回一个枚举器。

    Dictionary<int, string>.Enumerator myEnumerator =myDictionary.GetEnumerator();

// Call Eumerator.MoveNext() 方法,通过Eumerator.Current 属性遍历键值对;

    while(myEnumerator.MoveNext())

    {

       console.writeLine(myEnumerator.Current);

   }

  

7、仅遍历键 By Keys 属性 ,字典的属性:Dictionary<K,V>.keys 获取的是字典中Key的集合,类Dictionary<K,V>.KeyCollection 则是字典中Key的集合类。

  Dictionary<stringint>.KeyCollection keyCol = myDictionary.Keys;  

  foreach (string key in keyCol/*string key in myDictionary.Keys*/)  

 {  

   Console.WriteLine("Key = {0}", key);  

 }  

或者使用For语句通过索引遍历:

  for(int i =0; i<keyCol.Count; i++)

  {

      Console.WriteLine("Key = {0}", keyCol.ElementAt(i));

 }

 

8、仅遍历值By Valus属性, Dictionary<K,V>.Values 值的集合,传递给Value集合类Dictionary<K,V>.ValueCollection的对象。

  

 Dictionary<stringint>.ValueCollection valueCol = myDictionary.Values;  

 foreach (int value in valueCol)  

 {  

   Console.WriteLine("Value = {0}", value);  

  }  

  

9.移除指定的键值By Remove方法  

 

  myDictionary.Remove("C#");  

 if (myDictionary.ContainsKey("C#"))  

  {  

    Console.WriteLine("Key:{0},Value:{1}""C#", myDictionary["C#"]);  

  }  

  else  

  {  

    Console.WriteLine("不存在 Key : C#");  

      }  

  

在System.Collections.Generic命名空间中,与ArrayList相对应的泛型集合是List<T>,与 HashTable相对应的泛型集合是Dictionary<K,V>,其存储数据的方式与哈希表相似,通过键/值来保存元素,并具有泛型的全部特征,编译时检查类型约束,读取时无须类型转换。  

  

  电话本存储的例子中,使用Dictionary<K,V>来存储电话本信息,代码如下:  

  

  

Dictionary<string,TelNote> ht=new Dictionary<string,TelNote>();  

  

  在Dictionary<K,V>声明中,“<string,TelNote>”中的string表示集合中Key的类型,TelNote表示Value的类型,定义Dictionary<K,V>泛型集合中的方法如下:  

  

  

Dictionary<K,V> students=new Dictionary<K,V>();  

  

  其中“K”为占位符,具体定义时用存储键“Key”的数据类型代替,“V”也是占位符,用元素的值“Value”的数据类型代替,这样就在定义该集合时,声明了存储元素的键和值的数据类型,保证了类型的安全性。  

  Dictionary<K,V>中元素的操作方法与HashTable相似,添加元素,获取元素,删除元素,遍历集合元素的方法基本相同。  

  

Dictionary<K,V>和HashTable的区别  

Dictionary<K,V>和HashTable的相同点:添加元素,删除元素,通过键访问值的方法相同。  

Dictionary<K,V>和HashTable的不同点:  

Dictionary<K,V>对添加的元素具有类型约束,HashTable可添加任意类型的元素。  

Dictionary<K,V>不需要装箱、拆箱操作,HashTable添加时装箱,读取时拆箱。  

  

 在Dictionary<K,V>集合中,除了通过键获取值的方法外,还有一种TryGetValue(key)方法,可以通过键获取值,该方法返回值为布尔型,如果存在和键相对应的值,则返回true,否则返回false。避免了因获取不到相应值发生的异常。  

0 0
原创粉丝点击