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 7852501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:26:02+00:00 2026-06-02T19:26:02+00:00

I have writing a method which needs to be able to take an arbitrary

  • 0

I have writing a method which needs to be able to take an arbitrary number of fields of data, combine them in some way into a hashable object, then hash this object in a dictionary for later lookup.

So far the best algorithm I’ve come up with is to take ToHashCode() for each field, then join the resulting hashcodes into a string using some kind of separator character (such as “|”), and then use this resulting string as a unique key for the dictionary.

Does anyone know of a more efficient way to do this? I was thinking perhaps there is some way to take the hashcode of each field, and do some mathematical operation to combine them into a unique hashable number, but this was just a guess.

Thanks for any help.

EDIT:
I think people may be confused as to what exactly I mean. Tuples will not work in this situation because I need an arbitrary number of fields to be combined into a single hashable object. The number of fields is only known at runtime, not at design time.

The other solution given about combining all the hashcodes mathematically into a new hashcode also will not work, because I need an object that can be used as a key into a Dictionary. Using a hashcode as a key into a Dictionary is very dangerous I believe.

EDIT 2:
After thinking about this some more I think my original solution is not a good one. In the limiting case where there is a single field, my solution has degenerated into putting a string version of the hashcode into a Dictionary.

I think perhaps a better solution is to make a new type which takes an enumerable in its constructor, and implements GetHashCode(). The GetHashCode() function would then loop through each value of the enumerable and perform the usual type of accumulator logic in hash code functions. This way, the object can be stuck into a dictionary, hashset, etc. and behave as you would expect.

  • 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-02T19:26:03+00:00Added an answer on June 2, 2026 at 7:26 pm

    The key here was realizing that any arbitrarily sized collection of objects can be hashed by simply treating it as an IEnumerable whose hashcode depends on the contents of the enumeration.

    To do this I simply made a ValueAwareEnumerable class that implements IEnumerable. This class takes an enumerable in its only constructor. It then overrides GetHashCode() and Equals() so that they depend on the contents of the enumerable. The GetHashCode method is simply:

    public override int GetHashCode()
    {
        unchecked
        {
            int hash = 983;
            foreach (var item in _wrappedEnumerable)
               if(item != null)
                  hash = hash * 457 + item.GetHashCode();
            return hash;
        }
    }
    

    and Equals:

     public override bool Equals(object obj)
     {
         if (ReferenceEquals(null, obj)) return false;
         if (ReferenceEquals(this, obj)) return true;
         if (obj.GetType() != typeof (ValueAwareEnumerable<T>)) return false;
         return Equals((ValueAwareEnumerable<T>) obj);
     }
    
     public bool Equals(ValueAwareEnumerable<T> other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
    
         return _wrappedEnumerable.SequenceEqual(other);                               
     }
    

    The caveat here is that it depends on the order of the enumerable. If needed, it’s possible to make it order-independent by simply making GetHashCode() and Equals() sort the enumerable before iterating through it.

    To finish it off, just add in an extension method somewhere for good measure:

    public static IEnumerable<T> ToValueAwareEnumerable<T>(this IEnumerable<T> enumerable)
    {
       return new ValueAwareEnumerable<T>(enumerable);
    }
    

    And you can do things like:

    var dictionary = new Dictionary<IEnumerable<int>>();
    var veryImportantNumbers = new[] { 5, 8, 13, 20, 3, 100, 55, -5, 0 };
    dictionary[veryImportantNumbers.ToValueAwareEnumerable()] = "Pastrami";
    

    This will work for any data type, and even mixed data types, if you treat them as an IEnumerable<Object>.

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

Sidebar

Related Questions

I am writing some unit tests for an extension method I have written on
I'm writing an application which needs to make some work every 5 seconds, if
I'm writing an algorithm in OpenCL which needs a data structure temporarily (during execution)
I have a doubt about orkut's new URL Writing method. Earlier the home page
I have a method I'm writing that uses reflection to list a class's static
Does anyone have a template for writing a decent equals method - I remember
I'm writing a program in Java and I have a method with a header
I am writing an if/else alternative using a table driven method. I have the
I'm writing tetris right now, I have a method that drops figures one by
I have some trouble writing some code (I might be tired), so I need

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.