C#Dictionary泛型集合_03

using System;
using System.Collections.Generic;

namespace Dictionary泛型集合_03
{
    internal class Program
    {
        //static void Main(string[] args)
        //{
        //    //实例化对象,并初始化赋值
        //    Student objStu1 = new Student(1001, "张三");
        //    Student objStu2 = new Student(1002, "李四");
        //    Student objStu3 = new Student(1003, "王五");
        //    Student objStu4 = new Student(1004, "赵六");

        //    //创建Student类型的泛型集合
        //    List<Student> stuList = new List<Student>();
        //    //将Student的实例对象添加到泛型集合
        //    stuList.Add(objStu1);
        //    stuList.Add(objStu2);
        //    stuList.Add(objStu3);
        //    stuList.Add(objStu4);
        //    stuList.Add(new Student()
        //    {
        //        StudentId = 1009,
        //        StudentName = "小于"
        //    });

        //    //遍历集合
        //    foreach (Student item in stuList)
        //    {
        //        Console.WriteLine(item.StudentId + "\t" + item.StudentName);
        //    }

        //    //获取元素的个数
        //    Console.WriteLine("元素总数:{0}", stuList.Count);

        //    //删除一个元素
        //    stuList.Remove(objStu4);
        //    //插入一元素
        //    stuList.Insert(0, new Student(1007, "小海"));

        //    //集合初始化器和对象初始化器同时使用
        //    List<Student> List = new List<Student>()
        //    {
        //        new Student(){ StudentId=1005,StudentName="赵七"},
        //        new Student(){ StudentId=1006,StudentName="田七"}
        //    };

        //    //使用集合初始化器初始化泛型集合
        //    List<Student> list = new List<Student>() { objStu1, objStu2, objStu3 };
        //    List<string> nameList = new List<string>() { "小于", "小张", "小刘" };

        //    for (int i = 0; i < nameList.Count; i++)
        //    {
        //        Console.WriteLine(nameList[i]);
        //    }

        //}

        static void Main(string[] args)
        {
            //===========Dictionary<K,V>集合
            Student objStu1 = new Student(1001, "小张");
            Student objStu2 = new Student(1002, "小陈");
            Student objStu3 = new Student(1003, "小刘");

            //创建Dictionary泛型集合 
            Dictionary<string, Student> stus = new Dictionary<string, Student>();
            stus.Add("小王", objStu1);
            stus.Add("小董", objStu2);
            stus.Add("小3", objStu3);

            //Console.WriteLine(stus["小张"].StudentId);
            Console.WriteLine(stus["小董"].StudentId);

            foreach (string key in stus.Keys)
            {
                Console.WriteLine(key);
            }

            foreach (Student value in stus.Values)
            {
                Console.WriteLine(value.StudentName + "\t" + value.StudentId);
            }

            Console.ReadKey();
        }
    }

    class Student
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
        public string StudentAge { get; set; }

        public Student(int stuId, string stuName)
        {
            this.StudentId = stuId;
            this.StudentName = stuName;
        }
    }
}
posted @ 2026-05-26 15:36  人生就是修炼  阅读(3)  评论(0)    收藏  举报