Dictionary<string, Dictionary<string, Person>> dicFull字典的经典操作秒懂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Dictionary<string, Person>> dicFull = Person.GetFullDic();

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
Dictionary<string, Person> dicPerson = dicFull["South"];//怎么拆分一个字典中的数据
Person person = dicPerson["15"];//再导出其中的一个值
Console.WriteLine(person.Province + "," + person.Id + "," + person.Name + "," + person.Age);
sw.Stop();
Console.WriteLine("Cost " + sw.ElapsedMilliseconds.ToString() + " milliseconds");
Console.ReadLine();
}


}

public class Person
{
public string Province { get; set; }
public string Id { get; set; }

public string Name { get; set; }

public string Age { get; set; }
//用于直接附初值
public static Dictionary<string, Person> GetDic()
{
Dictionary<string, Person> dicPerson = new Dictionary<string, Person>();
for (int i = 0; i < 10; i++)
{
var key = i.ToString();
Person value = new Person()
{
Province = "SH",
Id = i.ToString(),
Name = "Fred" + i,
Age = "Age" + i
};
dicPerson.Add(key, value);
}

for (int i = 10; i < 20; i++)
{
var key = i.ToString();
Person value = new Person()
{
Province = "JS",
Id = i.ToString(),
Name = "WYQ" + i,
Age = "Age" + i
};

dicPerson.Add(key, value);
}

return dicPerson;
}

public static Dictionary<string, Dictionary<string, Person>> GetFullDic()
{
Dictionary<string, Dictionary<string, Person>> dic = new Dictionary<string, Dictionary<string, Person>>();
Dictionary<string, Person> dicPerson = Person.GetDic();
var key1 = "North";
dic.Add(key1, dicPerson);
var key2 = "South";
dic.Add(key2, dicPerson);

return dic;
}
}
}

 

 

//using System;

//namespace duchaotostring
//{
// public class Contact
// {
// protected string Name;
// protected string HomePhone;
// protected string busiPhone;
// protected string mobilePhone;

// public Contact(string name, string home, string busi, string mobile)
// {
// Name = name;
// HomePhone = home;
// busiPhone = busi;
// mobilePhone = mobile;
// }

// public override string ToString()
// {
// string temp = string.Format("姓名:{0},家庭电话:{1},办公电话:{2},移动电话:{3}\n",
// Name, HomePhone, busiPhone, mobilePhone);

// return temp;
// }

// }
//}

posted @   水木清扬  阅读(328)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2019-07-23 实战 迁移学习 VGG19、ResNet50、InceptionV3 实践 猫狗大战 问题
点击右上角即可分享
微信分享提示