浅谈Dictionary用法

一.基础篇                                  

1.Dictionary泛型类提供了从一组键到一组值的映射,即键和值的集合类。

2.Dictionary通过键来检索值的速度是非常快的,这是因为 Dictionary 类是作为一个哈希表来实现的。

3.定义方式:

 Dictionary<[Key], [Value]> openWith = new Dictionary<[Key], [Value]>();

 其中:Key代表此泛型类的键,不可重复。

    Value代表此泛型类中键对应的值。

    Key和Value可以用int,decimal,float等值类型,也可以用string,class等引用类型。

 举例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Test();
        }

        public void Test()
        {
            

            //Key为值类型   Value为值类型
            Dictionary<int, int> dicInt = new Dictionary<int, int>();
            //Key为值类型   Value为引用类型
            Dictionary<int, string> dicString = new Dictionary<int, string>();
            //Key为引用类型   Value为引用类型
            Dictionary<TestInfo, TestInfo> dicTestClass = new Dictionary<TestInfo, TestInfo>();
        }
    }

    public class TestInfo
    {
        public string ID { get; set; }

        public string Name { get; set; }

        public string Pwd { get; set; }
    }
}
View Code

4.添加键值对的方式:

 ①Dictionary.Add(Key,Value)方式: 

            //Key为值类型   Value为引用类型
            Dictionary<int, string> dicString = new Dictionary<int, string>();
            dicString.Add(0,"00001");
            dicString.Add(1, "00002");
            dicString.Add(2, "00003");
            dicString.Add(3, "00004");
View Code

 ②Dictionary[Key]=Value 方式:

            //Key为值类型   Value为引用类型
            Dictionary<int, string> dicString = new Dictionary<int, string>();
            dicString[0] = "00001";
            dicString[1] = "00002";
            dicString[2] = "00003";
            dicString[3] = "00004";
View Code

 ③两种方式对比:

  相同:二者皆可添加键值对。

  差异:第一种方式,当键不存在,相当于插入此键值对,当插入重复键时,则会引发ArgumentException类型的异常。

     第二种方式,当键不存在,相当于插入此键值对,当键存在时,相当于修改该键对应的值;当Dictionary[Key]取值

      时,如果此Key不存在,则会引发KeyNotFoundException异常。

  总结:添加键值对,推荐以第二种为主。

二.进阶篇                                         

1.ContainsKey(Key)判断键中是否含有此Key

            //Key为值类型   Value为引用类型
            Dictionary<int, string> dicString = new Dictionary<int, string>();
            dicString[0] = "00001";
            dicString[1] = "00002";
            dicString[2] = "00003";
            dicString[3] = "00004";

            if (dicString.ContainsKey(4))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('存在该键');</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('不存在该键');</script>");
            }
View Code

2.ContainsValue(Value)判断值中是否含有此Value

            //Key为值类型   Value为引用类型
            Dictionary<int, string> dicString = new Dictionary<int, string>();
            dicString[0] = "00001";
            dicString[1] = "00002";
            dicString[2] = "00003";
            dicString[3] = "00004";

            if (dicString.ContainsValue("00004"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('存在该值');</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('不存在该值');</script>");
            }
View Code

3.Foreach和KeyValuePair<Key, Value>配合取出所有键值对

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Key为值类型   Value为引用类型
            Dictionary<int, string> dicString = new Dictionary<int, string>();
            dicString[0] = "00001";
            dicString[1] = "00002";
            dicString[2] = "00003";
            dicString[3] = "00004";

            foreach (KeyValuePair<int, string> item in dicString)
            {
                Console.WriteLine("Key={0},Value={1}",item.Key,item.Value);
            }

            Console.ReadKey();
        }
    }
}
View Code

4.Dictionary.Keys,Dictionary.Values和foreach配合取出所有key,所有value。(也可以利用第三步中单独取key和value)

            //取出所有key
            foreach (int key in dicString.Keys)
            {
                Console.WriteLine("Key={0}", key);
            }
            //取出所有value
            foreach (string value in dicString.Values)
            {
                Console.WriteLine("Value={0}",value);
            }
View Code

 5.Dictionary.Clear()和Dictionary.Remove(Key)移除键值对

            //移除所有的键和值
            dicString.Clear();
            //移除键为0的键值对
            dicString.Remove(0);
View Code

6.Dictionary.Reverse()进行反转序列输出

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Key为值类型   Value为引用类型
            Dictionary<int, string> dicString = new Dictionary<int, string>();
            dicString[0] = "00001";
            dicString[1] = "00002";
            dicString[2] = "00003";
            dicString[3] = "00004";

            //原始排序输出
            foreach (KeyValuePair<int,string> item in dicString)
            {
                Console.WriteLine("Key={0},Value={1}",item.Key,item.Value);
            }

            //进行反转排序,存入iList
            IEnumerable<KeyValuePair<int,string>> iList = dicString.Reverse();

            Console.WriteLine("-----------------------------------------");
            //反转输出
            foreach (KeyValuePair<int, string> item in iList)
            {
                Console.WriteLine("Key={0},Value={1}", item.Key, item.Value);
            }

            Console.ReadKey();

            //输出结果如下:

            /*
              Key=0,Value=00001
              Key=1,Value=00002
              Key=2,Value=00003
              Key=3,Value=00004
              ------------------------------------------------
              Key=3,Value=00004
              Key=2,Value=00003
              Key=1,Value=00002
              Key=0,Value=00001
             */
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/xilipu31/p/4027656.html