Dictionary<T>的基本用法

1.Dictionary<T,T>增加键值对之前需要判断是否存在该键,如果已经存在该键而且不判断,抛出异常

2.Dictionary的Value为一个数组
3.Dicyionary的Value为一个类

 

                    #region Dictionary 用法总结
			Dictionary<string, string> pList = new Dictionary<string, string>();
			//1.增加键值对之前需要判断是否存在该键,如果已经存在该键而且不判断,抛出异常
			try
			{
				if (pList.ContainsKey("Item1Key") == false)
				{
					pList.Add("ItemsKey", "One");
				}
			}
			catch (System.Exception e)
			{
				Console.WriteLine("Eeero:{0}", e.Message);
			}
			//判断是否存在相应的Key并显示
			if (pList.ContainsKey("Item1Key"))
			{
				Console.WriteLine("Output:{0}", pList["Item1Key"]);
			}
			//便利遍历Key
			foreach (var key in pList.Keys)
			{
				Console.WriteLine("Output key:{0}", key);
			}
			//遍历Value
			foreach (string value in pList.Values)
			{
				Console.WriteLine("Output :value{0}", value);
			}
			//遍历key和value
			foreach (var dic in pList)
			{
				Console.WriteLine("Output key:{0},value:{1}", dic.Key,dic.Value);
			}
			//2.Dictionary的Value为一个数组
			Dictionary<string, string[]> AList = new Dictionary<string, string[]>();
			string[] Nums = { "One","Two","Three","Four","Five"};
			string[] Weeks = { "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
			AList.Add("Nums", Nums);
			AList.Add("Weeks", Weeks);
			foreach (var dic in AList)
			{
				Console.WriteLine("Output key:{0}", dic.Key);
				foreach (string value in dic.Value)
				{
					Console.WriteLine("Output value:{0}", value);
				}
			}
			//3.Dicyionary的Value为一个类
			Dictionary<string, Student> sList = new Dictionary<string, Student>();
			Student std = new Student();
			for (int i = 0; i < 5; i++)
			{
				std.c_id = i;
				std.c_name = Nums[i];
				std.c_code = "21012241" + i.ToString();
				std.c_remark = "电子信息科学与技术";
				std.c_db_status = 0;
				sList.Add(i.ToString(), std);
			}
			foreach (var s in sList)
			{
				Console.WriteLine("Output key:{0},c_id:{1},c_code:{2},c_db_status:{3},", s.Value.c_id, s.Value.c_code, s.Value.c_db_status);
			}
                    #endregion

 

Student类:

 

                public Student()
		{ }
		private int _c_id;
		private string _c_code;
		private string _c_name;
		private string _c_remark;
		private int _c_db_status = 0;
		/// <summary>
		/// 
		/// </summary>
		public int c_id
		{
			set { _c_id = value; }
			get { return _c_id; }
		}
		/// <summary>
		/// 
		/// </summary>
		public string c_code
		{
			set { _c_code = value; }
			get { return _c_code; }
		}
		/// <summary>
		/// 
		/// </summary>
		public string c_name
		{
			set { _c_name = value; }
			get { return _c_name; }
		}
		/// <summary>
		/// 
		/// </summary>
		public string c_remark
		{
			set { _c_remark = value; }
			get { return _c_remark; }
		}
		public int c_db_status
		{
			set { _c_db_status = value; }
			get { return _c_db_status; }
		}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fengzhilu000

送人玫瑰,手留余香!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值