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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:51:51+00:00 2026-06-07T00:51:51+00:00

Possible Duplicate: C# Why can equal decimals produce unequal hash values? I’ve come across

  • 0

Possible Duplicate:
C# Why can equal decimals produce unequal hash values?

I’ve come across an issue in my .NET 3.5 application (x86 or x64, I’ve tried both) where decimals with a different number of trailing zeros have different hash codes. For example:

decimal x = 3575.000000000000000000M;
decimal y = 3575.0000000000000000000M;

Console.WriteLine(x.GetHashCode());
Console.WriteLine(y.GetHashCode());
Console.WriteLine(x == y);
Console.WriteLine(x.GetHashCode() == y.GetHashCode());

Outputs the following on my machine:

1085009409
1085009408
True
False

I presume the difference in hash codes is down to the different internal representations of the two numbers caused by the differing scale factors.

Whilst I can work around the issue by removing the trailing zeros I always assumed that GetHashCode should return the same value for x and y, if x == y. Is this assumption wrong, or is this a problem with Decimal.GetHashCode?

EDIT: To be clear on versions I’m using Visual Studio 2008 SP1, .NET 3.5.

  • 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-07T00:51:53+00:00Added an answer on June 7, 2026 at 12:51 am

    This is a problem with Decimal.GetHashCode, for .NET Framework version 3.5 and lower. When two values are considered equal, they must return the same hash code, per the guidelines; in this case, decimal clearly does not. You should always expect two equal objects to have the same hash code.

    Per MSDN:

    If two objects compare as equal, the GetHashCode method for each
    object must return the same value.

    Reproducing

    I have tried your exact code against different versions of the .NET Framework, and the results are:

    ╔══════════════════╤══════════════════╗
    ║Framework version │ Hashcode equal ? ║
    ╟──────────────────┼──────────────────╢
    ║      2.0         │  No.             ║
    ║      3.0         │  No.             ║
    ║      3.5         │  No.             ║
    ║      4.0         │  Yes.            ║
    ║      4.5         │  Yes.            ║
    ╚══════════════════╧══════════════════╝
    

    In other words, it seems you stumbled upon a bug in the .NET framework, that was fixed with .NET Framework 4.

    The above results was reached using Visual Studio 2012 RC, using the property pages to switch the framework.

    Microsoft acknowledges the bug here.

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

Sidebar

Related Questions

Possible Duplicate: Escaping equal sign in properties files In a .properties file, can I
Possible Duplicate: Can you animate a height change on a UITableViewCell when selected? This
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: Can I overload an == operator on an Interface? I do understand
Possible Duplicate: Can anybody suggest the best image resize script in php? I'm still
Possible Duplicate: How can I convert ereg expressions to preg in PHP? I need
Possible Duplicate: How can I access local scope dynamically in javascript? Hi all. We
Possible Duplicate: How can I specify the latest time of day with DateTime I
Possible Duplicate: How can I parse JSON in Google App Engine? json module was
Possible duplicate: How can i give app store link in my apps before approving

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.