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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:49:05+00:00 2026-06-12T13:49:05+00:00

I’m not quite understand why does Object.GetHashCode() return different values for two identical byte

  • 0

I’m not quite understand why does Object.GetHashCode() return different values for two identical byte arrays, but returns equal values for not IEnumerable value type objects. For example:

byte e = 123;
Console.WriteLine(e.GetHashCode());

byte f = 123;
Console.WriteLine(f.GetHashCode());

output is

123
123

but when

byte[] a = new byte[3] { 1, 2, 3 };
Console.WriteLine(a.GetHashCode());

byte[] b = new byte[3] { 1, 2, 3 };
Console.WriteLine(b.GetHashCode());

output is

46104728
12289376

Why is it so, and how can I quickly compare two huge arrays without comparing their every element?

  • 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-12T13:49:07+00:00Added an answer on June 12, 2026 at 1:49 pm

    GetHashCode is not defined for array types – you have to implement your own hash algorithm.

    The value you see is actually based on the underlying reference and so two identical arrays will always have different hash codes, unless they are the same reference.

    For integral types 32-bits or less, the hash code is equal to the value as converted to a 32-bit integer. For the 64 bit integral type, Int64, the upper 32 bits are XORed with the lower 32 bits (there’s a shift in there also) for the hash code.

    So when it comes to trying to compare two arrays ‘quickly’, you have to do it yourself.

    You can use logic checks first – lengths are equal, start and end with the same byte value etc. Then you have a choice – either read byte – by – byte and compare values (or you can read 4 or 8 bytes at a time and use the BitConverter to convert blocks of bytes to Int32 or Int64 to make a single pair of values that might be quicker to check for equality) or use a general-purpose hash function to get a good guess of equality.

    For this purpose you can use an MD5 hash – it’s very quick to output a hash with MD5: How do I generate a hashcode from a byte array in C#?.

    Getting two identical hash values from such a hash function does not guarantee equality, but in general if you are comparing arrays of bytes within the same data ‘space’ you shouldn’t get a collision. By that I mean that, in general, examples of different data of the same type should nearly always produce different hashes. There’s a lot more around the net on this than I am qualified to explain.

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

Sidebar

Related Questions

I've tracked down a weird MySQL problem to the two different ways I was
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I need a function that will clean a strings' special characters. I do NOT
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.