Solved

GetHashCode method - Hashtable and Dictionary Collection Types - C#

Posted on 2012-04-06
7
461 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now