C# Dictionary 用法;

定义一个数组LonLat[] arr = new LonLat[2];可不可以将这个数组使用Dictionary,将键和键值关联起来,比如
Dictionary<int, LonLat[]>;LonLat是自定义的结构体?
C# 帮助中Dictionary<TKey, TValue>,Tvalue是值类型,数组算不算值类型?求详细解答

慕婉清6462132
浏览 618回答 2
2回答

撒科打诨

C# Dictionary用法总结1、用法1: 常规用  增加键值对之前需要判断是否存在该键,如果已经存在该键而且不判断,将抛出异常。所以这样每次都要进行判断,很麻烦,在备注里使用了一个扩展方法public static void DicSample1(){&nbsp;&nbsp;&nbsp; &nbsp; Dictionary<String, String> pList = new Dictionary<String, String>();&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (pList.ContainsKey("Item1") == false)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pList.Add("Item1", "ZheJiang");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (pList.ContainsKey("Item2")== false)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pList.Add("Item2", "ShangHai");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pList["Item2"] = "ShangHai";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (pList.ContainsKey("Item3") == false)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pList.Add("Item3", "BeiJiang");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; catch (System.Exception e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Error: {0}", e.Message);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; //判断是否存在相应的key并显示&nbsp; &nbsp; if (pList.ContainsKey("Item1"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Output: " + pList["Item1"]);&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; //遍历Key&nbsp; &nbsp; foreach (var key in pList.Keys)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Output Key: {0}", key);&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; //遍历Value&nbsp; &nbsp; foreach (String value in pList.Values)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Output Value: {0}", value);&nbsp; &nbsp; }&nbsp; &nbsp; //遍历Key和Value&nbsp; &nbsp; foreach (var dic in pList)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Output Key : {0}, Value : {1} ", dic.Key, dic.Value);&nbsp; &nbsp; }}  2、用法2:Dictionary的Value为一个数组/// <summary>/// Dictionary的Value为一个数组/// </summary>&nbsp;public static void DicSample2()&nbsp;{&nbsp; &nbsp; &nbsp;Dictionary<String, String[]> dic = new Dictionary<String, String[]>();&nbsp; &nbsp; &nbsp;String[] ZheJiang =&nbsp; { "Huzhou", "HangZhou", "TaiZhou" };&nbsp; &nbsp; &nbsp;String[] ShangHai = { "Budong", "Buxi" };&nbsp; &nbsp; &nbsp;dic.Add("ZJ", ZheJiang);&nbsp; &nbsp; &nbsp;dic.Add("SH", ShangHai);&nbsp; &nbsp; &nbsp;Console.WriteLine("Output :" + dic["ZJ"][0]);&nbsp;}3、用法3: Dictionary的Value为一个类//Dictionary的Value为一个类public static void DicSample3()&nbsp;{&nbsp; &nbsp; &nbsp;Dictionary<String, Student> stuList = new Dictionary<String, Student>();&nbsp; &nbsp; &nbsp;Student stu = null;&nbsp; &nbsp; &nbsp;for (int i = 0; i < 3; i++ )&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stu = new Student();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stu.Name = i.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stu.Name = "StuName" + i.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stuList.Add(i.ToString(), stu);&nbsp; &nbsp; &nbsp;}&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;foreach (var student in stuList)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine("Output : Key {0}, Num : {1}, Name {2}", student.Key, student.Value.Name, student.Value.Name);&nbsp; &nbsp; &nbsp;}&nbsp;}&nbsp; &nbsp;&nbsp;  Student类:public class Student{&nbsp; &nbsp; public String Num { get; set; }&nbsp; &nbsp; public String Name { get; set; }}&nbsp;4 备注:Dictionary的扩展方法使用/// <summary>/// Dictionary的扩展方法使用/// </summary>&nbsp;public static void DicSample4()&nbsp;{&nbsp; &nbsp; &nbsp;//1)普通调用&nbsp; &nbsp; &nbsp;Dictionary<int, String> dict = new Dictionary<int, String>();&nbsp; &nbsp; &nbsp;DictionaryExtensionMethodClass.TryAdd(dict, 1, "ZhangSan");&nbsp; &nbsp; &nbsp;DictionaryExtensionMethodClass.TryAdd(dict, 2, "WangWu");&nbsp; &nbsp; &nbsp;DictionaryExtensionMethodClass.AddOrPeplace(dict, 3, "WangWu");&nbsp; &nbsp; &nbsp;DictionaryExtensionMethodClass.AddOrPeplace(dict, 3, "ZhangWu");&nbsp; &nbsp; &nbsp;DictionaryExtensionMethodClass.TryAdd(dict, 2, "LiSi");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;//2)TryAdd 和 AddOrReplace 这两个方法具有较强自我描述能力,用起来很省心,而且也简单:&nbsp; &nbsp; &nbsp;dict.AddOrPeplace(20, "Orange");&nbsp; &nbsp; &nbsp;dict.TryAdd(21, "Banana");&nbsp; &nbsp; &nbsp;dict.TryAdd(22, "apple");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;//3)像Linq或jQuery一样连起来写&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;dict.TryAdd(10, "Bob")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.TryAdd(11, "Tom")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.AddOrPeplace(12, "Jom");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;//4) 获取值&nbsp; &nbsp; &nbsp;String F = "Ba";&nbsp; &nbsp; &nbsp;dict.TryGetValue(31, out F);&nbsp; &nbsp; &nbsp;Console.WriteLine("F : {0}",F);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;foreach (var dic in dict)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine("Output : Key : {0}, Value : {1}", dic.Key, dic.Value);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;//5)下面是使用GetValue获取值&nbsp; &nbsp; &nbsp;var v1 = dict.GetValue(111,null);&nbsp; &nbsp; &nbsp;var v2 = dict.GetValue(10,"abc");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;//6)批量添加&nbsp; &nbsp; &nbsp;var dict1 = new Dictionary<int,int>();&nbsp; &nbsp; &nbsp;dict1.AddOrPeplace(3, 3);&nbsp; &nbsp; &nbsp;dict1.AddOrPeplace(5, 5);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;var dict2 = new Dictionary<int, int>();&nbsp; &nbsp; &nbsp;dict2.AddOrPeplace(1, 1);&nbsp; &nbsp; &nbsp;dict2.AddOrPeplace(4, 4);&nbsp; &nbsp; &nbsp;dict2.AddRange(dict1, false);&nbsp;}&nbsp; &nbsp;&nbsp;  扩展方法所在的类public static class DictionaryExtensionMethodClass{&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// 尝试将键和值添加到字典中:如果不存在,才添加;存在,不添加也不抛导常&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; public static Dictionary<TKey, TValue> TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (dict.ContainsKey(key) == false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict.Add(key, value);&nbsp; &nbsp; &nbsp; &nbsp; return dict;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// 将键和值添加或替换到字典中:如果不存在,则添加;存在,则替换&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; public static Dictionary<TKey, TValue> AddOrPeplace<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; dict[key] = value;&nbsp; &nbsp; &nbsp; &nbsp; return dict;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// 获取与指定的键相关联的值,如果没有则返回输入的默认值&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return dict.ContainsKey(key)?dict[key] : defaultValue;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// 向字典中批量添加键值对&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="replaceExisted">如果已存在,是否替换</param>&nbsp; &nbsp; public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict, IEnumerable<KeyValuePair<TKey, TValue>> values, bool replaceExisted)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in values)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dict.ContainsKey(item.Key) == false || replaceExisted)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict[item.Key] = item.Value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return dict;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP