public override bool Equals(Object obj) { //Check for null and compare run-time types. if (obj == null || !(obj is UInt16UInt16O)) { return(false); } UInt16UInt16O item = (UInt16UInt16O)obj; return(this.EmpID == item.EmpID && this.Year == item.Year); }
static void Main(string[] args) { UInt16UInt16O Emp1 = new UInt16UInt16O(34, 1990); Emp1.PayIDs.Add(1); Emp1.PayIDs.Add(2); UInt16UInt16O Emp2 = new UInt16UInt16O(34, 1990, new List <byte> { 3, 4 }); if (Emp1 == Emp2) { Console.WriteLine("same"); } if (Emp1.Equals(Emp2)) { Console.WriteLine("Equals"); } Console.WriteLine("Emp1.GetHashCode " + Emp1.GetHashCode().ToString()); UInt16UInt16OCollection Employees = new UInt16UInt16OCollection(); Employees.Add(Emp1); //this would fail //Employees.Add(Emp2); Employees.Add(new UInt16UInt16O(35, 1991, new List <byte> { 1 })); Employees.Add(new UInt16UInt16O(35, 1992, new List <byte> { 1, 2 })); Employees.Add(new UInt16UInt16O(36, 1992)); Console.WriteLine(Employees.Count.ToString()); // reference by ordinal postion (note the is not the long key) Console.WriteLine(Employees[0].GetHashCode().ToString()); // reference by Int32 Int32 Console.WriteLine(Employees[35, 1991].GetHashCode().ToString()); Console.WriteLine("foreach"); foreach (UInt16UInt16O emp in Employees) { Console.WriteLine(string.Format("HashCode {0} EmpID {1} Year {2} NumCodes {3}", emp.GetHashCode(), emp.EmpID, emp.Year, emp.PayIDs.Count.ToString())); } Console.WriteLine("sorted"); foreach (UInt16UInt16O emp in Employees.OrderBy(e => e.EmpID).ThenBy(e => e.Year)) { Console.WriteLine(string.Format("HashCode {0} EmpID {1} Year {2} NumCodes {3}", emp.GetHashCode(), emp.EmpID, emp.Year, emp.PayIDs.Count.ToString())); } }