C#Dictionary 字典的用法
阿新 • • 發佈:2018-12-26
static void Main(string[] args)
{
Dictionary<int, string> ZiDian = new Dictionary<int,string>();
//增
ZiDian.Add(1,"張三");
ZiDian.Add(2,"李四");
ZiDian.Add(3,"王五");
ZiDian.Add(4,"趙六");
//查
Console.WriteLine("查");
string a = ZiDian[1];
Console.WriteLine("Name:{0}",a);
Console.WriteLine();
Console.WriteLine(ZiDian[2]);
Console.WriteLine();
foreach (var aa in ZiDian.Values)
{
Console.WriteLine(aa);
}
Console.WriteLine();
Console.WriteLine("刪");
//刪
ZiDian.Remove(3);
foreach (var aa in ZiDian.Values)
{
Console.WriteLine(aa);
}
Console.WriteLine();
Console.WriteLine("改");
//改
ZiDian.Add(3, "王五的兒子");
foreach (var aa in ZiDian.Values)
{
Console.WriteLine(aa);
}
}
{
Dictionary<int, string> ZiDian = new Dictionary<int,string>();
//增
ZiDian.Add(1,"張三");
ZiDian.Add(2,"李四");
ZiDian.Add(3,"王五");
ZiDian.Add(4,"趙六");
//查
Console.WriteLine("查");
string a = ZiDian[1];
Console.WriteLine("Name:{0}",a);
Console.WriteLine();
Console.WriteLine(ZiDian[2]);
Console.WriteLine();
foreach (var aa in ZiDian.Values)
{
Console.WriteLine(aa);
}
Console.WriteLine();
Console.WriteLine("刪");
//刪
ZiDian.Remove(3);
foreach (var aa in ZiDian.Values)
{
Console.WriteLine(aa);
}
Console.WriteLine();
Console.WriteLine("改");
//改
ZiDian.Add(3, "王五的兒子");
foreach (var aa in ZiDian.Values)
{
Console.WriteLine(aa);
}
}