Dictionary<>的用法

来源:互联网 发布:网络电视啥功能 编辑:程序博客网 时间:2024/06/14 05:15

Dictionary比HashTable更好用

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DictionaryUsage{    internal class Program    {        private static void Main(string[] args)        {            var dic = new Dictionary<int, string> {{1, "hang"}, {2, "hong"}, {3, "ximi"}};            //foreach (var temp in dic)            //{            //    Console.WriteLine("key: {0},value: {1}",temp.Key,temp.Value);            //}            //Console.WriteLine(dic);            //var a = dic.Where(var => var.Key == 2).GetEnumerator().Current.Value;            //Console.WriteLine(a);            //取值            Console.WriteLine(dic[1]);            //添加元素            dic.Add(4, "notepad.exe");            dic.Add(5, "paint.exe");            dic.Add(6, "paint.exe");            dic.Add(7, "wordpad.exe");            //更改值            dic[1] = "nihang1234";            //遍历字典            foreach (var temp in dic)            {                Console.WriteLine("key: {0},value: {1}", temp.Key, temp.Value);            }            //遍历键            foreach (var temp in dic.Keys)            {                Console.WriteLine(temp);            }            //遍历值            foreach (var temp in dic.Values)            {                Console.WriteLine(temp);            }            Console.ReadKey();        }    }}

0 0