========================================================== ========= Employeeid. CS (student ID)
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace consoleapplication7 {// The type of the key must be rewritten 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 ("the object cannot be blank "); return this. equals (OBJ as employeeid);} public override int gethashcode () {return 1;} public bool equals (employeeid other) {return this. id = Other. id ;}}}
========================================================== ========== Student. CS (student)
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; } }}
========================================================== ========= Main program
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, "Zhang Fei"); D. add (id1, stu1); var Id2 = new employeeid ("z002"); var stu2 = ne W student (Id2, "Guan Yu"); D. add (Id2, stu2); var ID3 = new employeeid ("z003"); var stu3 = new student (ID3, "Cao"); D. add (ID3, stu3); var ID4 = new employeeid ("z004"); var stu4 = new student (ID4, "Zhao Yun"); D. add (ID4, stu4); var id5 = new employeeid ("z005"); var stu5 = new student (id5, "Ma Chao"); D. add (id5, stu5); While (true) {string STR = console. readline (); If (Str. tolower () = "exit") return; student s; EMP Loyeeid Ei = NULL; Ei = new employeeid (STR); If (! D. trygetvalue (EI, out S) // you can determine whether the corresponding key value exists. {console. writeline ("This student is not found");} else {console. writeline (d [ei]. name );}}}}}
650) This. width = 650; "style =" width: 742px; Height: 797px; "Title =" lexicographic .png "alt =" wkiom1qfx20.20.r1faajyt3ethqu611.jpg "src =" http://s3.51cto.com/wyfs02/M01/48/28/wKiom1QFxqfh7R1fAAJyT3EThQU611.jpg "width =" 669 "Height =" 793 "/>
This article from the "programmer's home" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1548024
Dictionary --- dictionary <tkey, tvalue>