Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GetHashCode method - Hashtable and Dictionary Collection Types - C#

Posted on 2012-04-06
7
Medium Priority
?
476 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 20

Accepted Solution

by:
BuggyCoder earned 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

926 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