Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:19:11+00:00 2026-05-27T18:19:11+00:00

I came to this site searching for object comparison in Dictionary, and i came

  • 0

I came to this site searching for object comparison in Dictionary, and i came to know that overriding GetHashCode and Equals are a must for doing object comparison in C#.
Here is a piece of code that i have been trying to solve out, using FOREACH iteration Method. But my Boss says to do the same without using any iteration(maybe by using containskey or containsvalue method), due to performance issues. Any help is highly welcome..

  public class employee
    {
        public string empname { get; set; }
        public string location { get; set; }
        public double kinid { get; set; }
        public double managerKin { get; set; }
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }

    public class manager
    {
        public string managername { get; set; }
        public double kinid { get; set; }

        public override int GetHashCode() 
        { 
          return 17 * managername.GetHashCode() + kinid.GetHashCode();
        }
    }
    public class program
    {
        public static void Main()
        {
            employee emp = new employee();
            employee emp2 = new employee();
            manager mng = new manager();
            manager mng2 = new manager();

            emp.empname = "Deepak";
            emp.location = "Pune";
            emp.kinid = 36885;
            emp.managerKin = 007;


            emp2.empname = "Astha";
            emp2.location = "Pune";
            emp2.kinid = 30000;
            emp2.managerKin = 007;

            mng.kinid = 007;
            mng.managername = "Gaurav";
            mng2.kinid = 001;
            mng2.managername = "Surya";

            Dictionary<employee, manager> relations = new Dictionary<employee, manager>();
            relations.Add(emp, mng);
            relations.Add(emp2, mng2);

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("The Manager details are :");
            foreach (var element in relations)
            Console.WriteLine(" \n KINID : {0} \n  Manager'sName :                    {1}",element.Value.kinid, element.Value.managername);
            Console.WriteLine("Enter the details of the manager..");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("\nManager's Kin : ");
            double mkin = Convert.ToDouble(Console.ReadLine());

            Console.Write("Manager's Name : ");
            string mname = Console.ReadLine();

            manager mng1 = new manager();
            mng1.kinid = mkin;
            mng1.managername = mname;
            int hashvalue = 17 * mname.GetHashCode() + mkin.GetHashCode();



            #region BY USING FOREACH LOOP
            int i = 0;
            foreach (var element in relations)
            {
                if (element.Value.GetHashCode() == hashvalue)
                {
                    i += 1;
                    if (i == 1)
                    {
                        Console.WriteLine("The Following employees report to the Manager : {0}", mname);

                    }
                    Console.WriteLine(element.Key.empname + " " + element.Key.kinid + " " + element.Key.location + " " + element.Key.managerKin);

                }
            }
            if (i == 0)
            {
                Console.WriteLine("sorry the manager's details you entered \"{0}\" \"{1}\" does not exist in our database..", mng1.managername, mng1.kinid);

            }
            #endregion

            Console.ReadLine();
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:19:12+00:00Added an answer on May 27, 2026 at 6:19 pm

    For searching an object in a dictionary using the ContainsKey or ContainsValue keyword, compiler uses two implicit functions i.e. GetHashCode() and Equals(). So when we have an object for comparison, we need to Override both these methods !!

    Here is the code

    #region USING DICTIONARY TO STORE CLASS OBJECTS (check employee existence and print manager's name)
    public class employee
    {
        public string empname { get; set; }
        public string location { get; set; }
        public double kinid { get; set; }
        public double managerKin { get; set; }
    
        //public override bool Equals(object obj) // ANY OF THE TWO EQUALS METHOD WORKS.
        //{
        //    employee otheremployee;
        //    otheremployee = (employee)obj;
        //    return (otheremployee.kinid == this.kinid && otheremployee.location == this.location && otheremployee.empname == this.empname && otheremployee.managerKin == this.managerKin);
    
        //}
        public override bool Equals(object obj)   //When Running this entire code, put a break-point on both the Equals() and GetHashCode() methods, and see the execution flow.
        {
            employee otheremployee;
            otheremployee = (employee)obj;
            return (obj.GetHashCode() == otheremployee.GetHashCode());
    
        }
        public override int GetHashCode()    //When Running this entire code, put a break-point on both the Equals() and GetHashCode() methods, and see the execution flow.
        {
            //int temp = base.GetHashCode(); // DONT USE THIS
            //return base.GetHashCode();
            int temp = empname.GetHashCode() + location.GetHashCode() + kinid.GetHashCode() + managerKin.GetHashCode();
            return temp;
        }
    }
    
    public class manager
    {
        public string managername { get; set; }
        public double kinid { get; set; }
    
    
       
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
    }
    public class program
    {
        public static void Main()
        {
            employee emp = new employee();
            employee emp2 = new employee();
            manager mng = new manager();
            manager mng2 = new manager();
    
            emp.empname = "Deepak";
            emp.location = "Pune";
            emp.kinid = 36885;
            emp.managerKin = 007;
    
    
            emp2.empname = "Astha";
            emp2.location = "Pune";
            emp2.kinid = 30000;
            emp2.managerKin = 001;
    
            mng.kinid = 007;
            mng.managername = "Gaurav";
            mng2.kinid = 001;
            mng2.managername = "Surya";
    
            Dictionary<employee, manager> relations = new Dictionary<employee, manager>();
            relations.Add(emp, mng); // put a BreakPoint here and see the execution flow
            relations.Add(emp2, mng2);// put a BreakPoint here and see the execution flow
    
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("The Employee details are :");
            foreach (var element in relations)
                Console.WriteLine(" \n Employee Name : {0} \n Location : {1} \n Employee KinId : {2} \n Manager's KinId : {3} ",
                    element.Key.empname, element.Key.location, element.Key.kinid, element.Key.managerKin);
    
            Console.WriteLine("Enter the details of the Employee..");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("\nEmployee Name : "); string ename = Console.ReadLine();
            Console.Write("Location : "); string elocn = Console.ReadLine();
            Console.Write("Employee KinId : "); double ekinid = Convert.ToDouble(Console.ReadLine());
            Console.Write("Manager's ID : "); double emngr = Convert.ToDouble(Console.ReadLine());
            employee emp1 = new employee();
            emp1.empname = ename;
            emp1.location = elocn;
            emp1.kinid = ekinid;
            emp1.managerKin = emngr;
    
    
            int i = 0; // This variable acts as a indicator to find whether the Employee Key exists or not.
            if (relations.ContainsKey(emp1)) //Put a break point here and see the execution flow.
            {
                Console.WriteLine("the Employee : {0} exists..", emp1.empname);
                Console.WriteLine("the Employee reports to the following manager : {0} \n and the Manager's KinId is {1}.", (relations[emp1]).managername, relations[emp1].kinid);
                i = 1;
                Console.ReadLine();
            }
    
            if (i == 0)
            {
                Console.WriteLine("the details of the employee named {0} does not exist !!", emp1.empname);
                Console.ReadLine();
            }
    
    #endregion
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi while i go through this site (http://www.talenthouse.com/creativeinvites/preview/ae2dd0ca9ac9d1881f27132df12b6dd1/219), i came to see that while
I was searching through the posts on this site and i came across this:
I recently came across a question about sequence points in C++ at this site,
I'm a C++ programmer, and I was reading this site when I came across
Following the example on the RevenDB site , I came up with this: public
I am making a site that consumes a csv file this file can come
I came across this site ( http://demo.themezilla.com/memo/post-formats ) and I'm trying to figure out
I came across the term exploded development on this site... http://www.zeroturnaround.com/javarebel/support-matrix/ and remembered hearing
(I'm sure this must have been answered on this site already, but search gets
A while back I came onto this site to try and find a query,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.