Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GetHashCode method - Hashtable and Dictionary Collection Types - C#

Posted on 2012-04-06
7
Medium Priority
?
473 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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