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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:56:45+00:00 2026-05-26T16:56:45+00:00

My goal is to create a method with the following requirements: Output should be

  • 0

My goal is to create a method with the following requirements:

  • Output should be consistent across different app domains (but which are running the same version of the .Net framework)
  • Objects of different types should not generate the same hash
  • Collisions are extremely unlikely
  • The method will be called fairly frequently, so should not be too slow

The implementations that I’m considering look something like:

private static long GenerateHash<TKey>(TKey key)
{
    long typeHash = typeof(TKey).GetHashCode();
    long keyHash = key.GetHashCode();
    return (typeHash << 32) + keyHash;
}

and

private static long GenerateHash<TKey>(TKey key)
    {
        using (var stream = new MemoryStream())
        {
            var formatter = new BinaryFormatter(); // Or other serialiser
            formatter.Serialize(stream, key);
            stream.Seek(0, SeekOrigin.Begin);
            var hashAlgorithm = new SuitableHashAlgorithm(); // Not real class, need to find/write a hash algorithm that can compute 64 bit hashes...
            var hash = hashAlgorithm.ComputeHash(stream);
            return BitConverter.ToInt64(hash, 0);
        }
}

Note, the possible nullness of key is not a consideration.

Any comments and potential pitfalls of these implementations welcomed along with any other possible ones.

Thanks

  • 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-05-26T16:56:46+00:00Added an answer on May 26, 2026 at 4:56 pm

    It appears that the requirements cannot be met with the stated method signature, thanks all for your comments, particularly @Marc Gravell.

    I’ll introduce a suitable interface with a UniqueId property which all keys will implement.

    I was hoping to avoid this in order to maintain backwards compatibility, but hey ho, you can’t always get what you want!

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

Sidebar

Related Questions

quandry is - which of the following two method performs best Goal - get
The goal is to create a mock class which behaves like a db resultset.
How can I invoke following method while I have not TRootEntity , but have
I am creating a windows form app with the following goal: get a list
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
The goal: To create a .NET dll i can reference from inside SQL Server
Hi here's the goal: to create an automatic bit of content that toggles open
Duplicate: PHP validation/regex for URL My goal is create a PHP regex for website
my goal is to create a sort of Javascript library, if you could call
My goal is to create an efficient structure to store the most relevant entries

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.