网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

字典---Dictionary<TKey,TValue>

=================================================EmployeeID.cs(学生编号类)

创新互联沟通电话:13518219792,为您提供成都网站建设网页设计及定制高端网站建设服务,创新互联网页制作领域10余年,包括成都混凝土搅拌罐车等多个行业拥有多年的网站设计经验,选择创新互联,为企业保驾护航!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
    //作为键的类型必须要重写GetHashCode()
    public class EmployeeID : IEquatable
    {
        public string ID { get; private set; }
        public EmployeeID(string s)
        {
            ID = s;
        }

        public override bool Equals(object obj)
        {
            if (obj == null) throw new ArgumentException("对象不能为空");
            return this.Equals(obj as EmployeeID);
        }
        public override int GetHashCode()
        {
            return 1;
        }
        public bool Equals(EmployeeID other)
        {
            return this.ID == other.ID;
        }
    }
}

=================================================Student.cs(学生类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
    public class Student
    {
        public string Name { get; private set; }
        public EmployeeID ID { get; private set; }
        public Student(EmployeeID id, string name)
        {
            this.Name = name;
            this.ID = id;
        }
    }
}

=================================================主程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary d = new Dictionary();
            var ID1 = new EmployeeID("Z001");
            var Stu1 = new Student(ID1, "张飞");
            d.Add(ID1, Stu1);
            var ID2 = new EmployeeID("Z002");
            var Stu2 = new Student(ID2, "关羽");
            d.Add(ID2, Stu2);
            var ID3 = new EmployeeID("Z003");
            var Stu3 = new Student(ID3, "曹操");
            d.Add(ID3, Stu3);
            var ID4 = new EmployeeID("Z004");
            var Stu4 = new Student(ID4, "赵云");
            d.Add(ID4, Stu4);
            var ID5 = new EmployeeID("Z005");
            var Stu5 = new Student(ID5, "马超");
            d.Add(ID5, Stu5);
            while (true)
            {
                string str = Console.ReadLine();
                if (str.ToLower() == "exit") return;
                Student s;
                EmployeeID ei = null;
                ei = new EmployeeID(str);
                if (!d.TryGetValue(ei, out s))//判断是否存在键对应的相关的值
                {
                    Console.WriteLine("没有找到该学生");
                }
                else
                {
                    Console.WriteLine(d[ei].Name);
                }
            }
                
        }
    }
}

字典---Dictionary<TKey,TValue>


网站标题:字典---Dictionary<TKey,TValue>
转载源于:http://bjjierui.cn/article/gepsjp.html

其他资讯