Dictionary的常用方法
class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("one", "hello");
        dict.Add("two", "world");
        dict.Add("three", "gogo");

        //判断字典的key是否存在
        if (dict.ContainsKey("two"))
        {
            //根据键值取值
            Console.WriteLine(dict["two"]);
        }

        //输出字典的长度
        Console.WriteLine(dict.Count);

        //遍历字典
        foreach (var i in dict)
        {
            Console.WriteLine(i.Key + ":" + i.Value);
        }

        Console.ReadKey();
    }
}

首页 我的博客
粤ICP备17103704号