這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)C#中Dictionary如何使用,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)公司專(zhuān)注于民勤企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。民勤網(wǎng)站建設(shè)公司,為民勤等地區(qū)提供建站服務(wù)。全流程按需求定制網(wǎng)站,專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
用法1: 常規(guī)用
增加鍵值對(duì)之前需要判斷是否存在該鍵,如果已經(jīng)存在該鍵而且不判斷,將拋出異常。所以這樣每次都要進(jìn)行判斷,很麻煩,在備注里使用了一個(gè)擴(kuò)展方法
public static void DicSample1()
{
Dictionary<String, String> pList = new Dictionary<String, String>();
try
{
if (pList.ContainsKey("Item1") == false)
{
pList.Add("Item1", "ZheJiang");
}
if (pList.ContainsKey("Item2")== false)
{
pList.Add("Item2", "ShangHai");
}
else
{
pList["Item2"] = "ShangHai";
}
if (pList.ContainsKey("Item3") == false)
{
pList.Add("Item3", "BeiJiang");
}
}
catch (System.Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
//判斷是否存在相應(yīng)的key并顯示
if (pList.ContainsKey("Item1"))
{
Console.WriteLine("Output: " + pList["Item1"]);
}
//遍歷Key
foreach (var key in pList.Keys)
{
Console.WriteLine("Output Key: {0}", key);
}
//遍歷Value
foreach (String value in pList.Values)
{
Console.WriteLine("Output Value: {0}", value);
}
//遍歷Key和Value
foreach (var dic in pList)
{
Console.WriteLine("Output Key : {0}, Value : {1} ", dic.Key, dic.Value);
}
}
用法2:Dictionary的Value為一個(gè)數(shù)組
/// <summary>
/// Dictionary的Value為一個(gè)數(shù)組
/// </summary>
public static void DicSample2()
{
Dictionary<String, String[]> dic = new Dictionary<String, String[]>();
String[] ZheJiang = { "Huzhou", "HangZhou", "TaiZhou" };
String[] ShangHai = { "Budong", "Buxi" };
dic.Add("ZJ", ZheJiang);
dic.Add("SH", ShangHai);
Console.WriteLine("Output :" + dic["ZJ"][0]);
}
用法3: Dictionary的Value為一個(gè)類(lèi)
//Dictionary的Value為一個(gè)類(lèi)
public static void DicSample3()
{
Dictionary<String, Student> stuList = new Dictionary<String, Student>();
Student stu = null;
for (int i = 0; i < 3; i++ )
{
stu = new Student();
stu.Name = i.ToString();
stu.Name = "StuName" + i.ToString();
stuList.Add(i.ToString(), stu);
}
foreach (var student in stuList)
{
Console.WriteLine("Output : Key {0}, Num : {1}, Name {2}", student.Key, student.Value.Name, student.Value.Name);
}
}
Student類(lèi):
public class Student
{
public String Num { get; set; }
public String Name { get; set; }
}
Dictionary的擴(kuò)展方法使用
/// <summary>
/// Dictionary的擴(kuò)展方法使用
/// </summary>
public static void DicSample4()
{
//1)普通調(diào)用
Dictionary<int, String> dict = new Dictionary<int, String>();
DictionaryExtensionMethodClass.TryAdd(dict, 1, "ZhangSan");
DictionaryExtensionMethodClass.TryAdd(dict, 2, "WangWu");
DictionaryExtensionMethodClass.AddOrPeplace(dict, 3, "WangWu");
DictionaryExtensionMethodClass.AddOrPeplace(dict, 3, "ZhangWu");
DictionaryExtensionMethodClass.TryAdd(dict, 2, "LiSi");
//2)TryAdd 和 AddOrReplace 這兩個(gè)方法具有較強(qiáng)自我描述能力,用起來(lái)很省心,而且也簡(jiǎn)單:
dict.AddOrPeplace(20, "Orange");
dict.TryAdd(21, "Banana");
dict.TryAdd(22, "apple");
//3)像Linq或jQuery一樣連起來(lái)寫(xiě)
dict.TryAdd(10, "Bob")
.TryAdd(11, "Tom")
.AddOrPeplace(12, "Jom");
//4) 獲取值
String F = "Ba";
dict.TryGetValue(31, out F);
Console.WriteLine("F : {0}",F);
foreach (var dic in dict)
{
Console.WriteLine("Output : Key : {0}, Value : {1}", dic.Key, dic.Value);
}
//5)下面是使用GetValue獲取值
var v1 = dict.GetValue(111,null);
var v2 = dict.GetValue(10,"abc");
//6)批量添加
var dict1 = new Dictionary<int,int>();
dict1.AddOrPeplace(3, 3);
dict1.AddOrPeplace(5, 5);
var dict2 = new Dictionary<int, int>();
dict2.AddOrPeplace(1, 1);
dict2.AddOrPeplace(4, 4);
dict2.AddRange(dict1, false);
}
擴(kuò)展方法所在的類(lèi)
public static class DictionaryExtensionMethodClass
{
/// <summary>
/// 嘗試將鍵和值添加到字典中:如果不存在,才添加;存在,不添加也不拋導(dǎo)常
/// </summary>
public static Dictionary<TKey, TValue> TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
{
if (dict.ContainsKey(key) == false)
dict.Add(key, value);
return dict;
}
/// <summary>
/// 將鍵和值添加或替換到字典中:如果不存在,則添加;存在,則替換
/// </summary>
public static Dictionary<TKey, TValue> AddOrPeplace<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
{
dict[key] = value;
return dict;
}
/// <summary>
/// 獲取與指定的鍵相關(guān)聯(lián)的值,如果沒(méi)有則返回輸入的默認(rèn)值
/// </summary>
public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
{
return dict.ContainsKey(key)?dict[key] : defaultValue;
}
/// <summary>
/// 向字典中批量添加鍵值對(duì)
/// </summary>
/// <param name="replaceExisted">如果已存在,是否替換</param>
public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict, IEnumerable<KeyValuePair<TKey, TValue>> values, bool replaceExisted)
{
foreach (var item in values)
{
if (dict.ContainsKey(item.Key) == false || replaceExisted)
dict[item.Key] = item.Value;
}
return dict;
}
}
上述就是小編為大家分享的C#中Dictionary如何使用了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱(chēng):C#中Dictionary如何使用
URL分享:http://jinyejixie.com/article10/jjihgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、App設(shè)計(jì)、小程序開(kāi)發(fā)、自適應(yīng)網(wǎng)站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)