Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:53:23+00:00 2026-06-12T08:53:23+00:00

I have a hashtable base class and I am creating different type of hashtable

  • 0

I have a hashtable base class and I am creating different type of hashtable by deriving from it. I only allow it to accept objects that implement my IHashable interface.For example –

class LinearProbingHashTable<T> : HashTableBase<T> where T: IHashable
{
...
...
...
}

interface IHashable
{
    /**
     * Every IHashable implementation should provide an indentfying value for use in generating a hash key.
     */
    int getIdentifier();
}

class Car : IHashable
{
    public String Make { get; set; }
    public String Model { get; set; }
    public String Color { get; set; }
    public int Year { get; set; }

    public int getIdentifier()
    {
        /// ???
    }
}

Can anyone suggest a good method for generating an identifier for the car that can be used by the hash function to place it in the hash table?

I am actually really looking for a general purpose solution to generating an id for any given class. I would like to have a base class for all classes, HashableObject, that implements IHashable and its getIdentifier method. So then I could just derive from HashableObject which would automatically provide an identifier for any instances. Which means I wouldn’t have to write a different getIdentifier method for every object I add to the hashtable.

public class HashableObject : IHashable
{
  public int getIdentifier()
  {
    // Looking for code here that would generate an id for any object...
  }
}

public class Dog : HashableObject
{
  // Dont need to implement getIdentifier because the parent class does it for me
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:53:24+00:00Added an answer on June 12, 2026 at 8:53 am

    I would split the problem in two:

    1. How to generate hash codes of primitive types: strings, integers etc.
    2. How to combine multiple hash codes into one hash code

    using (1) and then (2) you can generate the hash code of any class or structure.

    The naive way to do (1) for strings is to add the code of all characters in the string:

    public static int getStringIdentifier(string str)
    {
       int result = 0;
       foreach (char c in str) {
         result += (int)c;
       }
       return result;
    }
    

    Similar naive algorithms can be used for other basic data types (that are all array of bytes in the end..).

    The naive way to do (2) is to simply combine the various hash codes with XOR:

    public int getIdentifier() 
    { 
      return getStringIdentifier(Make) ^ getStringIdentifier(Model) ^ getStringIdentifier(Color);     
    } 
    

    These algorithms will work, but won’t generate good distributions of the hash code values – i.e. there will be collisions.

    If you want better algorithms you can have a look at how the .NET framework does it – here is the source code of the class used intenally to combine multiple hash codes, and here is the source code of the String class – including String.GetHashCode().

    As you can see they are variants of the naive one above, with different starting values and more complex combinations.

    If you want a single method that works on different classes the way to do it is to use reflection to detect all the primitive fields contained in the class, compute their hash code using the primitive functions and then combine them.
    It is tricky and extermely .NET-specific though – my preference would be to create methods handling the primitive types and then just re-define getIdentifier() for each class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hashtable . values() method returns values in some order different from
I have one key but different values for that key. I tried HashTable but
I have an hashtable that I add to when I am creating jobs with
I have a Hashtable that contains numerous EventLog objects. In my FormClosed event, I
I have a class that maintans a reference to a Hashtable and serializes/deserializes that
I just noticed the HashTable objects have a Contains and CotainsKey method, with same
I have a list of string values that I want add to a hashtable
I have a Hashtable that maps strings to ints. Strings are unique, but several
I have a synchronized Hashtable with int as the key, and a custom class
I have a Hashtable of type Hashtable I've loaded several strings as keys, one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.