您的位置:首页 > 编程语言 > C#

C# Dictionary

2016-04-05 15:05 197 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 字典
{
class Program
{
static void Main(string[] args)
{
//Hashtable 键值对集合
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "123");
dic.Add(2, "456");
dic.Add(3, "789");
//通过索引赋值,会覆盖原有值
dic[1] = "111";

//两种遍历的方式
foreach (KeyValuePair<int, string> kv in dic)
{
Console.WriteLine("{0}------{1}", kv.Key, kv.Value);
}

foreach (var item in dic.Keys)
{
Console.WriteLine("{0}----------{1}", item, dic[item]);

}
Console.ReadKey();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# dictionary