C# 字典Dictionary

Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射。通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary<TKey, TValue> 类是作为一个哈希表来实现的。检索速度取决于为 TKey 指定的类型的哈希算法的质量。TValue可以是值类型,数组,类或其他。 Dictionary是一种变种的HashTable,它采用一种分离链接散列表的数据结构来解决哈希冲突的问题。

using System;
using System.Collections.Generic;
namespace App{
    class MyClass {
        public static void Main(string[] args){
            Dictionary<int,int> dict = new Dictionary<int,int>();
            dict.Add(1,1);
            Console.WriteLine(dict[1]);
        }
    }
}

 C# 6语法,声明带有int件和string值的字典可以初始化一下:

            Dictionary<int,string> dict = new Dictionary<int,string>(){
                [1]="abs",
                [4]="hhh"
            };
            //dict.Add(1,1);
            Console.WriteLine(dict[1]);

 

posted @ 2019-04-16 15:15  liliyou  阅读(217)  评论(0编辑  收藏  举报