这个主要是用来总结有关Dictionary添加元素的方法
第一种:简单用法
Dictionary<string, int> 或者 Dictionary<string, string>
第一步:我首先定义了一个类:DictiaonaryTest,然后在类中定义了一个全局变量,接下来完成如下功能测试
1、元素的添加
2、遍历字典中的key值并输出
3、遍历字典中的val值并输出
4、遍历字典,遍历字典,并将key值和val值都输出
public class DictiaonaryTest { //定义全局变量 并初始化字典 Dictionary<string, int> dic = new Dictionary<string, int>() { {"C#", 2 }, {"VB", 1 }, {"C", 0 }, {"C++", -1} }; #region 01 常规用法 添加元素 /// <summary> /// 为字典添加元素 /// </summary> public void TestDicAdd() { Dictionary<string, int> dicSample = new Dictionary<string, int>(); dicSample.Add("C#", 2); dicSample.Add("VB", 1); dicSample.Add("C", 0); dicSample.Add("C++", -1); // 判断是否存在相应的key并显示 key对应的值 if (dicSample.ContainsKey("C#")) { int p = dicSample["C#"]; MessageBox.Show(p.ToString()); } } /// <summary> /// 遍历字典中的key /// </summary> public void TestDicEachKey() { int i = 0; string str = string.Empty; foreach (var dickey in dic.Keys) { str += "key" + i + ":" + dickey + " "; i++; } MessageBox.Show(str); } /// <summary> /// 遍历字典中的val /// </summary> public void TestDicEachValue() { int i = 0; string str = string.Empty; foreach (var dicVal in dic.Values) { str += "val" + i + ":" + dicVal + " "; i++; } MessageBox.Show(str); } public void TestDicEach() { int i = 0; string str = string.Empty; foreach (var dicitem in dic) { str += "key" + i + ":" + dicitem.Key + " " + "val" + i + ":" + dicitem.Value + " "; i++; } MessageBox.Show(str); } #endregion }
第二种: Dictionary的Value为一个数组
Dictionary<String, String[]> dicArr = new Dictionary<String, String[]>() { { "浙江",new string[] {"Huzhou", "HangZhou", "TaiZhou"}}, { "上海",new string[] { "Budong", "Buxi" }} }; public void TestDicArrEach() { int i = 0; string str = string.Empty; foreach (var dicitem in dicArr) { str += "key" + i + ":" + dicitem.Key + " " + "val" + i + ":"; foreach (var item in dicitem.Value) { str += item + " "; } i++; } MessageBox.Show(str); }
第三种:value 是个对象
Dictionary<int, Dog> dicObj = new Dictionary<int, Dog>() { { 1,new Dog() { DogId = 1, DogName = "小白", DogAge = 10, DogToy = "小球"}}, { 2,new Dog() { DogId = 2, DogName = "小黑", DogAge = 20, DogToy = "狗骨头" }}, { 3,new Dog() { DogId = 3, DogName = "小花", DogAge = 30, DogToy = "小球"}} }; public void TestDiffMethod() { Console.WriteLine("添加"); dicObj.TryAdd(4, new Dog() { DogId = 4, DogName = "老妖精", DogAge = 40, DogToy = "篮球" }); //此处写了一个扩展方法 dicObj.ForeachEx(); Console.WriteLine("添加Or替换1"); dicObj.AddOrReplace(2, new Dog() { DogId = 2, DogName = "小熊", DogAge = 20, DogToy = "蜂蜜" }); //此处写了一个扩展方法 dicObj.ForeachEx(); Console.WriteLine("添加Or替换2"); dicObj.AddOrReplace(5, new Dog() { DogId = 2, DogName = "小熊", DogAge = 20, DogToy = "蜂蜜" }); dicObj.ForeachEx(); Console.WriteLine("根据键值获取某个值:"); Dog d = dicObj.GetValByKey(5, new Dog() { }); Console.WriteLine(d.ToString()); Console.WriteLine("批量添加:"); Dictionary<int, Dog> dicObj2 = new Dictionary<int, Dog>() { { 3,new Dog() { DogId = 1, DogName = "小白", DogAge = 10, DogToy = "小球"}}, { 4,new Dog() { DogId = 2, DogName = "小黑", DogAge = 20, DogToy = "狗骨头" }}, { 5,new Dog() { DogId = 3, DogName = "小花", DogAge = 30, DogToy = "小球"}} }; dicObj.AddBatch(dicObj2, false); dicObj.ForeachEx();
}
写了一个类用来扩展类:DictionaryExtend
public static class DictionaryExtend { /// <summary> /// 向字典中添加元素 /// </summary> /// <typeparam name="TKey">key类型</typeparam> /// <typeparam name="TVal">val类型</typeparam> /// <param name="dic"></param> /// <param name="key">key</param> /// <param name="val">val</param> /// <returns></returns> public static Dictionary<TKey, TVal> TryAdd<TKey, TVal>(this Dictionary<TKey, TVal> dic, TKey key, TVal val) { if (!dic.ContainsKey(key)) { dic.Add(key, val); } return dic; } /// <summary> /// 将键和值添加或替换到字典中:如果不存在,则添加;存在,则替换 /// </summary> public static Dictionary<TKey, TVal> AddOrReplace<TKey, TVal>(this Dictionary<TKey, TVal> dict, TKey key, TVal value) { dict[key] = value; return dict; } /// <summary> /// 根据key值获取key对应的val值 /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TVal"></typeparam> /// <param name="dic"></param> /// <param name="key"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static TVal GetValByKey<TKey, TVal>(this Dictionary<TKey, TVal> dic, TKey key, TVal defaultValue) { return dic.ContainsKey(key) ? dic[key] : defaultValue; } /// <summary> /// 向字典中批量添加键值对 /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TVal"></typeparam> /// <param name="dic"></param> /// <param name="diclist"></param> /// <param name="replaceExisted">如果已存在,是否替换</param> /// <returns></returns> public static Dictionary<TKey, TVal> AddBatch<TKey, TVal>(this Dictionary<TKey, TVal> dic, Dictionary<TKey, TVal> diclist, bool replaceExisted = false) { foreach (var item in diclist) { if (!dic.ContainsKey(item.Key) || replaceExisted) { dic[item.Key] = item.Value; } } return dic; } public static void ForeachEx<Int, TVal>(this Dictionary<Int, TVal> dic) { int i = 0; string str = string.Empty; if (dic.Count > 0) { foreach (var item in dic) { str += "key" + i + ":" + item.Key + " Val" + i + ":" + item.Value.ToString() + " "; i++; } } Console.WriteLine(str); }
}