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
最新推荐文章于 2024-08-29 17:46:16 发布