C# 泛型 Dictionary

此类在 .NET Framework 2.0 版中是新增的。

命名空间:System.Collections.Generic

程序集:mscorlib(在 mscorlib.dll 中)

表示键和值的集合: a、键必须是唯一的,而值不需要唯一的  b、键和值都可以是任何类型(比如:string, int, 自定义类型,等等) 


常用属性:

    名称    说明
    Comparer     获取用于确定字典中的键是否相等的 IEqualityComparer<T>。
    Count        获取包含在 Dictionary<TKey, TValue> 中的键/值对的数目。
    Item         获取或设置与指定的键相关联的值。
    Keys         获取包含 Dictionary<TKey, TValue> 中的键的集合。
    Values       获取包含 Dictionary<TKey, TValue> 中的值的集合。


常用方法:
    名称    说明
    Add                 将指定的键和值添加到字典中。
    Clear               从 Dictionary<TKey, TValue> 中移除所有的键和值。
    ContainsKey         确定 Dictionary<TKey, TValue> 是否包含指定的键。
    ContainsValue       确定 Dictionary<TKey, TValue> 是否包含特定值。
    Equals(Object)      确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
    Finalize            允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
    GetEnumerator       返回循环访问 Dictionary<TKey, TValue> 的枚举器。
    GetHashCode         用作特定类型的哈希函数。 (继承自 Object。)
    GetObjectData       实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary<TKey, TValue> 实例所需的数据。
    GetType             获取当前实例的 Type。 (继承自 Object。)
    MemberwiseClone     创建当前 Object 的浅表副本。 (继承自 Object。)
    OnDeserialization    实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。
    Remove              从 Dictionary<TKey, TValue> 中移除所指定的键的值。
    ToString            返回表示当前对象的字符串。 (继承自 Object。)
    TryGetValue         获取与指定的键相关联的值。


用法:

// 实例化一个对象
Dictionary<string, string> dic= new Dictionary<string, string>();
// 添加元素
dic.Add("1","a");
dic.Add("2","b");
dic.Add("3","c");
// 取值
string value = dic["2"];  // value ==b
// 取出由value值对应的key值
var keys = dic.Where(q => q.Value == "b").Select(q => q.Key);
// 更改值
dic["2"] = "abc";
string value = dic["2"] ;  // value == abc
//删除元素
dic.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
        Console.WriteLine("Key \"doc\" is not found.");
}
//判断键存在
if (dic.ContainsKey("abcd")) // True  or flase
{ 
	// ...
}
//遍历key
foreach (string key in dic.Keys)
{
	Console.WriteLine("Key = {0}", key);
}
//遍历value
foreach (string value in dic.Values)
{
	Console.WriteLine("value= {0}", value);
}
//遍历value
Dictionary<string, string>.ValueCollection valueColl = dic.Values;
foreach (string s in valueColl)
{
	Console.WriteLine("Second Method, Value = {0}", s);
}
//遍历字典
foreach (var item in dic)     //foreach(KeyValuePair<string, string> item in dic)
{
        Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value);
}
//如下代码也可循环dic集合
Dictionary<string, string>.Enumerator em = dic.GetEnumerator();
//循环读取
while (em.MoveNext())
{
    //值
     string value = em.Current.Value;
    //键
     string key = em.Current.Key;
}
// 利用for遍历
 for (int i = 0; i < sn_ip.Count; i++)
 {      
	var item = sn_ip.ElementAt(i);
        var tempkey = item.key;
	var tempvalue = item.value;
}


当参数为自定义类型时

首先定义类

class CodePage
{
	public int Code { get { return _Code; } set { _Code = value; } } private int _Code;
	public string Page { get { return _Page; } set { _Page = value; } } private string _Page;
} 
//声明并添加元素
Dictionary<int, CodePage> MyType = new Dictionary<int, CodePage>();
for (int i = 1; i <= 9; i++)
{    
	CodePage element = new CodePage();
        element.Code = i * 100;
        element.Page = "http://www.doucube.com/" + i.ToString() + ".html";
        MyType.Add(i, element);
    }
//遍历元素
foreach (KeyValuePair<int, CodePage> kvp in MyType)
{
	Console.WriteLine("Index {0} Code:{1} Page:{2}", kvp.Key, kvp.Value.Code, kvp.Value.Page);
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值