當前位置:成語大全網 - 新華字典 - c# 中Dictionary怎麽用?

c# 中Dictionary怎麽用?

我們用的比較多的非泛型集合類主要有 ArrayList類 和 HashTable類。我們經常用HashTable 來存儲將要寫入到數據庫或者返回的信息,在這之間要不斷的進行類型的轉化,增加了系統裝箱和拆箱的負擔,如果我們操縱的數據類型相對確定的化 用 Dictionary<TKey,TValue> 集合類來存儲數據就方便多了,例如我們需要在電子商務網站中存儲用戶的購物車信息( 商品名,對應的商品個數)時,完全可以用 Dictionary<string, int> 來存儲購物車信息,而不需要任何的類型轉化。

下面是簡單的例子,包括聲明,填充鍵值對,移除鍵值對,遍歷鍵值對

Dictionary<string, string> myDic = new Dictionary<string, string>();

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<string, string> 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<string, string>.ValueCollection values = myDic.Values;

foreach (string s in values)

{

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

}

常用的屬性和方法如下: 常用屬性

屬性說明

Comparer

獲取用於確定字典中的鍵是否相等的 IEqualityComparer。

Count

獲取包含在 Dictionary中的鍵/值對的數目。

Item

獲取或設置與指定的鍵相關聯的值。

Keys

獲取包含 Dictionary中的鍵的集合。

Values

獲取包含 Dictionary中的值的集合。

常用的方法 方法說明

Add

將指定的鍵和值添加到字典中。

Clear

從 Dictionary中移除所有的鍵和值。

ContainsKey

確定 Dictionary是否包含指定的鍵。

ContainsValue

確定 Dictionary是否包含特定值。

Equals

已重載。 確定兩個 Object 實例是否相等。 (從 Object 繼承。)

GetEnumerator

返回循環訪問 Dictionary的枚舉數。

GetHashCode

用作特定類型的哈希函數。GetHashCode 適合在哈希算法和數據結構(如哈希表)中使用。 (從 Object 繼承。)

GetObjectData

實現 System.Runtime.Serialization.ISerializable 接口,並返回序列化 Dictionary實例所需的數據。

GetType

獲取當前實例的 Type。 (從 Object 繼承。)

OnDeserialization

實現 System.Runtime.Serialization.ISerializable接口,並在完成反序列化之後引發反序列化事件。

ReferenceEquals

確定指定的 Object實例是否是相同的實例。 (從 Object 繼承。)

Remove

從 Dictionary中移除所指定的鍵的值。

ToString

返回表示當前 Object的 String。 (從 Object 繼承。)

TryGetValue

獲取與指定的鍵相關聯的值。

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

namespace Test

{

class Program

{

static void Main(string[] args)

{

char[] chars = new char[] { 'a', 'b', 'c', 'a', 'b', 'c', 'c', 'd', 'd', 'e', 'c' };

//存儲字符和字符個數

Dictionary<char, int> dic = new Dictionary<char, int>();

foreach (char c in chars)

{

if (dic.ContainsKey(c))

dic[c] += 1;

else

dic.Add(c, 1);

}

//排序

List<KeyValuePair<char, int>> list = new List<KeyValuePair<char, int>>();

foreach (KeyValuePair<char, int> p in dic)

{

int count = list.Count;

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

{

if (p.Value > list[i].Value)

{

list.Insert(i, p);

break;

}

else

{

if (p.Value == list[i].Value)

{

if (p.Key < p.Key)

{

list.Insert(i, p);

break;

}

else

continue;

}

else

continue;

}

}

if (count == list.Count)

list.Add(p);

}

//顯示字符

string s = "";

foreach (KeyValuePair<char, int> p in list)

{

s += new string(p.Key, p.Value);

}

Console.WriteLine(s);

Console.Read();

}

}

}

摘自csdn 希望能幫助妳