=================================================EmployeeID.cs(學(xué)生編號(hào)類)
創(chuàng)新互聯(lián)溝通電話:13518219792,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),創(chuàng)新互聯(lián)網(wǎng)頁(yè)制作領(lǐng)域10余年,包括成都混凝土攪拌罐車等多個(gè)行業(yè)擁有多年的網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),選擇創(chuàng)新互聯(lián),為企業(yè)保駕護(hù)航!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication7 { //作為鍵的類型必須要重寫GetHashCode() public class EmployeeID : IEquatable<EmployeeID> { public string ID { get; private set; } public EmployeeID(string s) { ID = s; } public override bool Equals(object obj) { if (obj == null) throw new ArgumentException("對(duì)象不能為空"); return this.Equals(obj as EmployeeID); } public override int GetHashCode() { return 1; } public bool Equals(EmployeeID other) { return this.ID == other.ID; } } }
=================================================Student.cs(學(xué)生類)
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<EmployeeID, Student> d = new Dictionary<EmployeeID, Student>(); var ID1 = new EmployeeID("Z001"); var Stu1 = new Student(ID1, "張飛"); d.Add(ID1, Stu1); var ID2 = new EmployeeID("Z002"); var Stu2 = new Student(ID2, "關(guān)羽"); 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))//判斷是否存在鍵對(duì)應(yīng)的相關(guān)的值 { Console.WriteLine("沒(méi)有找到該學(xué)生"); } else { Console.WriteLine(d[ei].Name); } } } } }
本文標(biāo)題:字典---Dictionary<TKey,TValue>
分享路徑:http://www.chinadenli.net/article28/gepsjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、企業(yè)建站、網(wǎng)頁(yè)設(shè)計(jì)公司、建站公司、定制開發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)