Solved

GetHashCode method - Hashtable and Dictionary Collection Types - C#

Posted on 2012-04-06
7
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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