C#中Dictionary的用法

要使用Dictionary集合,需要导入C#泛型命名空间

System.Collections.Generic(程序集:mscorlib)

Dictionary的描述

1、从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成

2、任何键都必须是唯一的

3、键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值

4、Key和Value可以是任何类型(string,int,custom class 等)

Dictionary常用用法:以 key 的类型为 int , value的类型为string 为例

1、创建及初始化

Dictionary<int,string>myDictionary=newDictionary<int,string>();

2、添加元素

myDictionary.Add(1,“C#”);

myDictionary.Add(2,“C++”);

myDictionary.Add(3,“ASP.NET”);

myDictionary.Add(4,“MVC”);

3、通过Key查找元素

if(myDictionary.ContainsKey(1))

{

Console.WriteLine(“Key:{0},Value:{1}”,“1”, myDictionary[1]);

}

4、通过KeyValuePair遍历元素

foreach(KeyValuePair<int,string>kvp in myDictionary)

{

Console.WriteLine(“Key = {0}, Value = {1}”,kvp.Key, kvp.Value);

}

5、仅遍历键 Keys 属性

Dictionary<int,string>.KeyCollection keyCol=myDictionary.Keys;

foreach(intkeyinkeyCol)

{

Console.WriteLine(“Key = {0}”, key);

}

6、仅遍历值 Valus属性

Dictionary<int,string>.ValueCollection valueCol=myDictionary.Values;

foreach(stringvalueinvalueCol)

{

Console.WriteLine(“Value = {0}”, value);

}

7、通过Remove方法移除指定的键值

myDictionary.Remove(1);

if(myDictionary.ContainsKey(1))

{

Console.WriteLine(“Key:{0},Value:{1}”,“1”, myDictionary[1]);

}

else

{

Console.WriteLine(“不存在 Key : 1”);

}

其它常见属性和方法的说明:

Comparer: 获取用于确定字典中的键是否相等的 IEqualityComparer。

Count: 获取包含在 Dictionary中的键/值对的数目。

Item: 获取或设置与指定的键相关联的值。

Keys: 获取包含 Dictionary中的键的集合。

Values: 获取包含 Dictionary中的值的集合。

Add: 将指定的键和值添加到字典中。

Clear: 从 Dictionary中移除所有的键和值。

ContainsKey: 确定 Dictionary是否包含指定的键。

ContainsValue: 确定 Dictionary是否包含特定值。

GetEnumerator: 返回循环访问 Dictionary的枚举数。

GetType: 获取当前实例的 Type。 (从 Object 继承。)

Remove: 从 Dictionary中移除所指定的键的值。

ToString: 返回表示当前 Object的 String。 (从 Object 继承。)

TryGetValue: 获取与指定的键相关联的值。

附上完整代码及运行结果

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建泛型哈希表,Key类型为int,Value类型为string
            Dictionary<int, string> myDictionary = new Dictionary<int, string>();
            //1.添加元素
            myDictionary.Add(1, "a");
            myDictionary.Add(2, "b");
            myDictionary.Add(3, "c");
            //2.删除元素
            myDictionary.Remove(3);
            //3.假如不存在元素则添加元素
            if (!myDictionary.ContainsKey(4))
            {
                myDictionary.Add(4, "d");
            }
            //4.显示容量和元素个数
            Console.WriteLine("元素个数:{0}",myDictionary.Count);
            //5.通过key查找元素
            if (myDictionary.ContainsKey(1))
            {
                Console.WriteLine("key:{0},value:{1}","1", myDictionary[1]);
                Console.WriteLine(myDictionary[1]);            
            }
            //6.通过KeyValuePair遍历元素
            foreach (KeyValuePair<int,string>kvp in myDictionary)
            {
                Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);

            }
            //7.得到哈希表键的集合
            Dictionary<int, string>.KeyCollection keyCol = myDictionary.Keys;
                //遍历键的集合
                foreach (int n in keyCol)
                {
                    Console.WriteLine("key={0}", n);                
                }
            //8.得到哈希表值的集合
            Dictionary<int, string>.ValueCollection valCol = myDictionary.Values;
                //遍历值的集合
                foreach( string s in valCol)
                {
                Console.WriteLine("value:{0}",s);
                }
            //9.使用TryGetValue方法获取指定键对应的值
            string slove = string.Empty;
            if (myDictionary.TryGetValue(5, out slove))
            {
                Console.WriteLine("查找结果:{0}", slove);
            }
            else
            {
                Console.WriteLine("查找失败");
            }
            //10.清空哈希表
            //myDictionary.Clear();
            Console.ReadKey();
        }
    }
}

运行结果:在这里插入图片描述

转自
https://www.cnblogs.com/jaejaking/p/5301288.html
https://www.cnblogs.com/ChenMM/p/9479987.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值