using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字典
{
class Program
{
static void Main(string[] args)
{
//1.创建字典泛型集合(Dictionary<K,V>)
Dictionary<string, string> dic = new Dictionary<string, string>();
//集合长度可通过"集合名.Count"访问
//2.添加数据
dic.Add("百度","baidu");//添加数据
dic.Add("腾讯", "Tencent");
dic.Add("淘宝", "Taobao");
//dic.Add("百度", "rg");//错误,不能添加同键名的数据
//3.查询数据,取值,通过集合名[键名]
Console.WriteLine(dic.Count);
Console.WriteLine(dic["百度"]);
//4.删除数据
//dic.Remove("淘宝");
//Console.WriteLine(dic.Count);
//5.修改数据
dic["腾讯"] = "马化腾";
Console.WriteLine("修改腾讯后:"+dic["腾讯"]);
//6.遍历数据
Console.WriteLine("=============遍历Key:");
foreach(var temp in dic.Keys)
{
Console.WriteLine(temp);
}
Console.WriteLine("===========遍历Value:");
foreach (var temp in dic.Values)
{
Console.WriteLine(temp);
}
Console.WriteLine("============一次遍历:");
foreach(var temp in dic.Keys)
{
Console.WriteLine(temp + " " + dic[temp]);
}
Console.ReadKey();
}
}
}
字典Dictionary基本使用
最新推荐文章于 2022-04-27 14:56:28 发布