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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:38:55+00:00 2026-05-26T22:38:55+00:00

The following code produces the output of 46104728: using System; namespace TestApplication { internal

  • 0

The following code produces the output of 46104728:

using System;

namespace TestApplication
{
    internal static class Program
    {
        private static void Main()
        {
            Type type = typeof(string);
            Console.WriteLine(type.GetHashCode());
            Console.ReadLine();
        }
    }
}

But so does this:

using System;

namespace TestApplication
{
    internal static class Program
    {
        private static void Main()
        {
            Type type = typeof(Program);
            Console.WriteLine(type.GetHashCode());
            Console.ReadLine();
        }
    }
 }

Yet on http://ideone.com it produces varying results for each type. This issue has been reproduced on more than one system now. I’m using .NET 4.0 right now.

  • 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-26T22:38:56+00:00Added an answer on May 26, 2026 at 10:38 pm

    You’ve run into what you believe to be a problem, however, if you were to look at their hash codes in the same execution you’ll find that they’re not identical but instead rely on their order of usage:

    Console.WriteLine("{0} {1:08X}", typeof(string), typeof(string).GetHashCode());
    Console.WriteLine("{0} {1:08X}", typeof(Program), typeof(Program).GetHashCode());
    // System.String 02BF8098
    // Program 00BB8560
    

    If I run that same program again, swapping their order:

    Console.WriteLine("{0} {1:08X}", typeof(Program), typeof(Program).GetHashCode());
    Console.WriteLine("{0} {1:08X}", typeof(string), typeof(string).GetHashCode());
    // Program 02BF8098
    // System.String 00BB8560
    

    This is a non-issue at runtime as the returned values do not violate the rules for implementing Object.GetHashCode.

    But, as you noted this behavior seems curious!

    I delved into the source and found the implementation of Type.GetHashCode is foisted off onto MemberInfo.GetHashCode, which is again foisted off onto Object.GetHashCode which calls RuntimeHelpers.GetHashCode(this).

    It is at this point that the trail goes cold, however, my assumption is the inner workings of that method creates a new value, mapped per instance, based on the order of calls.

    I tested this hypothesis by running the same code above with two instances of Program (after adding a property to identify them):

    var b = new Program() { Name = "B" };
    var a = new Program() { Name = "A" };
    Console.WriteLine("{0} {1:08X}", a.Name, a.GetHashCode());
    Console.WriteLine("{0} {1:08X}", b.Name, b.GetHashCode());
    // A 02BF8098
    // B 00BB8560
    

    Thus, for classes which do not explicitly override Object.GetHashCode, instances will be assigned a seemingly predictable hash value based on the order in which they call GetHashCode.


    Update: I went and looked at how Rotor/Shared Source CLI handles this situation, and I learned that the default implementation calculates and stores a hash code in the sync block for the object instance, thus ensuring the hash code is generated only once. The default computation for this hash code is trivial, and uses a per-thread seed (wrapping is mine):

    // ./sscli20/clr/src/vm/threads.h(938)
    // Every thread has its own generator for hash codes so that we
    // won't get into a situation where two threads consistently give
    // out the same hash codes.
    // Choice of multiplier guarantees period of 2**32
    // - see Knuth Vol 2 p16 (3.2.1.2 Theorem A).
    

    So if the actual CLR follows this implementation it would seem any differences seen in hash code values for objects are based on the AppDomain and Managed Thread which created the instance.

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

Sidebar

Related Questions

Consider the following code: #include <iostream> using namespace std; class Test { static int
The following code produces a ListView with the names of the customers: private void
The following code produces the output xyz a = %w{x y z} print a.to_s
The following code produces wrong and inconsistent output with gcc (4.1.2 20080704) but correct
code sample taken from MSDN public class Test { public static void Main() {
Confusing question, I know. Given the following: class Test { public static void GenericFunc<T>(T
Consider the following code: echo $this->Form->year('expiry',date('Y'),date('Y')+2) This produces output something like: 2013 2012 2011
The following code console.log(Math.pow(2, 53)); console.log(Math.pow(2, 53) + 1); produces exactly same output for
The following code Console.WriteLine({0:%h} hours {0:%m} minutes, new TimeSpan(TimeSpan.TicksPerDay)); produces this output: 0 hours
I have a select query that currently produces the following results: Description Code Price

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.