Dictionary 的使用
使用可重复使用的袋子、瓶子和餐具,减少一次性用品的使用。 #生活知识# #节约#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DictionaryUsageDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1、key value 为字符串");
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("key1", "学习C SHARP");
dict.Add("key2", "学习JAVA");
dict.Add("key3", "学习C++");
Console.ReadLine();
//
Console.WriteLine("2、遍历KEY");
foreach (var key in dict.Keys)
{
Console.WriteLine("输出KEY:" + key);
}
Console.ReadLine();
Console.WriteLine("3、遍历 VALUE");
foreach (var value in dict.Values)
{
Console.WriteLine("输出VALUE:"+value);
}
Console.ReadLine();
Console.WriteLine("4、遍历key,value");
foreach (var dic in dict)
{
Console.WriteLine("输出key:{0} , value:{1}", dic.Key, dic.Value);
}
Console.ReadLine();
Console.WriteLine("5、Dictionary的Value为一个数组");
Dictionary<string, string[]> dicArr = new Dictionary<string, string[]>();
string[] arr1 = { "北京", "上海", "重庆", "海南" };
string[] arr2 = { "广东","广西","山西"};
dicArr.Add("zhs", arr1);
dicArr.Add("sf", arr2);
foreach (var value in dicArr.Values)
{
string[] varr = value;
foreach (var str in varr) {
Console.Write(str+" \t");
}
Console.WriteLine();
}
Console.ReadLine();
Console.WriteLine("6、Dictionary的Value为一个类");
Dictionary<string, Person> dicClass = new Dictionary<string, Person>();
dicClass.Add("p1", new Person(1, "jacky", "广东"));
dicClass.Add("p2", new Person(1, "tomer", "山西"));
dicClass.Add("p3", new Person(1, "menk", "北京"));
//
foreach (var Person in dicClass)
{
Console.WriteLine("输出:ID:{0} 姓名:{1} 地址:{2}", Person.Value.id, Person.Value.name, Person.Value.addr);
}
Console.ReadLine();
}
}
class Person
{
public int id { get; set; }
public string name { get; set; }
public string addr { get; set; }
public Person(int id, string name, string addr)
{
this.id = id;
this.name = name;
this.addr = addr;
}
}
}
网址:Dictionary 的使用 https://www.yuejiaxmz.com/news/view/536141
相关内容
java ikanalyzer 动态扩展分词如何开发自有品牌App的本地定时功能
便利性的意思
语音识别的架构
衣食住行软件哪个好用 好用的衣食住行软件推荐
No.=Number的来历
推荐系统的个性化推荐:如何实现用户特征的精细化
第1456期: 乐趣是健康生活的秘诀(上)
生活用品名称大全
Android网络优化:提升用户体验的七大策略