using System.Collections.Generic;
public static void Main()
private static void Program4()
Department Department1 = new Department() { DepartmentID = 1, DepartmentName = "HR" };
Department Department2 = new Department() { DepartmentID = 2, DepartmentName = "Finance" };
Employee Employee1 = new Employee() { EmployeeID = 1, DepartmentID = 1, EmployeeName = "Karan" };
Employee Employee2 = new Employee() { EmployeeID = 2, DepartmentID = 2, EmployeeName = "Arjun" };
Dictionary<Department, Employee> dict = new Dictionary<Department, Employee>()
{ Department1, Employee1},
{ Department2, Employee2}
Department tempDepartment = default(Department);
foreach (var item in dict)
if (item.Key.Equals(Employee1))
tempDepartment = item.Key;
if (tempDepartment != default(Department))
Console.WriteLine("Found EmployeeID {0} DepartmentID {1} (Name {2}).",
Employee1.EmployeeID, tempDepartment.DepartmentID, Employee1.EmployeeName);
Console.WriteLine("Employee {0} not found.", Employee1.EmployeeName);
public class Department : IEquatable<Employee>
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public bool Equals(Employee employee)
if (this.DepartmentID == employee.DepartmentID)
public override bool Equals(Object obj)
Employee employeeObj = obj as Employee;
return Equals(employeeObj);
public override int GetHashCode()
return this.DepartmentID.GetHashCode();
public int EmployeeID { get; set; }
public int DepartmentID { get; set; }
public string EmployeeName { get; set; }