Solved

GetHashCode method - Hashtable and Dictionary Collection Types - C#

Posted on 2012-04-06
7
464 Views
Last Modified: 2012-04-09
Hi,

Can you please let me know about:

GetHashCode method in Hashtable and Dictionary Collection Types - C#

Please explain to me

1) what it is ?
2) Why we need it ?
3) How to use it ?

Thanks
0
Comment
Question by:milani_lucie
  • 5
  • 2
7 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37818328
in short every key value collection internally operates on an array to achieve o(1) time complexity. Each key you pass in is converted to an array index using technique called hashing. The value is then stored at this index. next time when you retrieve it again by passing the key, the key again get hashed to return the index and the value is retreived from that index.

Now Hashtables and Dictionaries can even treat instances of your types(objects) as keys. Now since hashing works on an assumption that item to be hashed has to be integer so we need a way for our objects to return an integer when they are to be used as keys. When you override gethashcode, you work an algorithm to provide key(int) for your object and so it get hashed and can be used as key.

Here are more details:-
http://msdn.microsoft.com/en-us/library/ms379571%28v=vs.80%29.aspx#datastructures20_2_topic5
http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx
0
 

Author Comment

by:milani_lucie
ID: 37822327
Can you please provide me the code: How to implement it ?

Thanks
0
 

Author Comment

by:milani_lucie
ID: 37823871
Any help with sample code - Thanks
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37824131
public class Customer
{
private long CustomerId;
private string CustomerName;

public Customer(long id, string Name)
{
CustomerId=id;
CustomerName=Name;
}

public override int GetHashCode()
{
return CustomerId.GetHashCode();
}
}

Open in new window

0
 

Author Comment

by:milani_lucie
ID: 37824206
How to declare the Dictionary Key of type Object (Customer) ? Can you please provide some sample code how to declare it ?

Thanks
0
 

Author Comment

by:milani_lucie
ID: 37824227
Dictionary<Customer, string> dictionary = new Dictionary<Customer, string>();

Am i correct ?

Thanks
0
 

Author Comment

by:milani_lucie
ID: 37824243
Complete Code:

public class Customer
{
      private long CustomerId;
      private string CustomerName;

      public Customer(long id, string Name)
      {
            CustomerId=id;
            CustomerName=Name;
      }

      public override int GetHashCode()
      {
            return CustomerId.GetHashCode();
      }
}

Dictionary<Customer, string> dict = new Dictionary<Customer, string>();

dict.Add(cust1, "Hello World");
dict.Add(cust2, "Bye World");

Am i correct ?

Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question