C#集合,字典的运用

三个题解释所有

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

namespace FuXIC8
{
class Program
{
static void Main(string[] args)
{
#region 求交集
/*List<int> list1 = new List<int>();
list1.Add(1);
list1.Add(2);
list1.Add(3);
list1.Add(4);
List<int> list2 = new List<int>();
list2.Add(2);
list2.Add(3);
list2.Add(4);
list2.Add(5);
for (int i = 0; i < list1.Count; i++)
{
for (int j = 0; j < list2.Count; j++)
{
if (list1[i] == list2[j])
{
Console.WriteLine(list1[i]);
}
}
}*/
#endregion
#region 遍历输出字典
/*Dictionary<string, string> stu = new Dictionary<string, string>();
stu.Add("001", "张三");
stu.Add("002", "李四");
stu.Add("003", "王二");

//遍历字典的几种方法,还有一种直接取值,这里不做过多演示

foreach (KeyValuePair<string, string> item in stu)
{
Console.WriteLine("学号:{0},姓名:{1}", item.Key, item.Value);
}
foreach (var item in stu)
{
Console.WriteLine("学号:{0},姓名:{1}", item.Key, item.Value);
}
foreach (var item in stu.Keys)
{
Console.WriteLine("学号:{0},姓名:{1}", item, stu[item]);
}
List<string> test = new List<string>(stu.Keys);
for (int i = 0; i < stu.Count; i++)
{
Console.WriteLine("学号:{0},姓名:{1}", test[i], stu[test[i]]);
}*/
#endregion
}
}
#region 定义一个学生类,其有姓名,年龄两个字段,年龄要求不能低于10,不能高于50
class Student
{
public string Name { get; set; }

public int Age;

public int age
{
get
{
return Age;
}
set
{
if (value > 10 && value < 50)
{
Age = value;
}
else
{
throw new Exception("年龄不符合");
}
}
}
}
#endregion
}

 

posted @ 2021-04-19 11:18  薛小谦  阅读(207)  评论(0编辑  收藏  举报