字典类

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

namespace CollectionEx
{
    class Program
    {
        static void Main(string[] args)
        {
            /*ArrayList arraylist = new ArrayList();
            arraylist.Add(1);
            arraylist.Add(1.1);
            arraylist.Add(true);
            arraylist.Add("abc");
            Console.WriteLine(arraylist[2]);

            arraylist[3] = "hahaha";

            foreach (var item in arraylist)
                Console.WriteLine(item);*/


            /*//泛型 强行类 类型检查
            List list = new List();
            list.Add(1);
            list.Add(2);
            list.AddRange(new int[] { 1, 2, 3, 4, 5 });
            Console.WriteLine(list[1]);
            list[3] = 10;
            Console.WriteLine(list.Count + "," + list.Capacity);

            foreach (int item in list)
                Console.WriteLine(item);

            list.Sort();
            list.Reverse();
            foreach (int item in list)
                Console.WriteLine(item);*/


            Dictionary dict = new Dictionary();
            dict.Add("003", new Student
            { Name = "tian", Sno = "003" });
            dict.Add("006", new Student
            { Name = "mary", Sno = "006" });
            Student student = dict["003"];
            Console.WriteLine(student);
            dict["003"] = new Student() { Sno = "003", Name = "joh" };
            if (!dict.ContainsKey("007"))
                Console.WriteLine("no this student");
            foreach (var key in dict.Keys)
                Console.WriteLine(dict[key]);
            //foreach (KeyValuePair item in dict)
               // Console.WriteLine(item.Value); 遍历输出,输出结果与上输出相同
           
        }

        internal class Student
        {
            public string Sno { get; set; }
            public string Name { get; set; }
            public override string ToString()
            {
                return $"Name:{Name},Sno:{Sno}";
            }
        }
    }
}

 

你可能感兴趣的:(C#语言)