3

Let's say I have a few classes:

MyClass1() {}

MyChild1() : MyClass1 {}

MyChild2() : MyClass2 {}

MyGrandchild1() : MyChild2 {}

etc.

I know that GetHashCode() by itself, does not guarantee uniqueness between any two different Objects, but I'm interested does that apply for any two Types as well? i.e.:

(1) is there a chance that: typeof(MyClass1).GetHashCode() == typeof(MyGrandchild1).GetHashCode() will return true?

(2) if there's a chance for (1): is there a chance that typeof(MyClass1) == typeof(MyGrandchild1) will return true?

(3) worst case scenario: is there a chance that typeof(int) == typeof(long) will return true?

EDIT I forgot to ask about case (4) typeof(int).GetHashCode() == typeof(long).GetHashCode(), is there a chance for that to return true?

avance70
  • 787
  • 1
  • 11
  • 22
  • 5
    1. Yes; 2. No; 3. No – Dmitry Bychenko Oct 19 '15 at 11:56
  • `GetHashCode()` returns integer value. So there is limited count of possible unique values. On other hand there is no limit for different types you can have. So answer is no, there is no guaranteed uniqueness. `GetHashCode()` for different types might be same – Sergey Berezovskiy Oct 19 '15 at 11:57
  • Possible duplicate of [.NET: Is Type.GetHashCode guaranteed to be unique?](http://stackoverflow.com/questions/7458139/net-is-type-gethashcode-guaranteed-to-be-unique) – Fabjan Oct 19 '15 at 11:58
  • 2
    @Fabjan if it was only (1) I would agree that it is a full duplicate but as (2) and (3) are also part of the question it sounds like the OP is confused there about equality of types and nonequality of them which makes this question different imho. – Thomas Oct 19 '15 at 11:59
  • @SergeyBerezovskiy _So there is only about 32k possible unique values._ Isn't [`int`](https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx) a 32-bit type? There sould be 2^32 possible unique values not only 32k (HashCodes are allowed to be negative). – Sebastian Schumann Oct 19 '15 at 12:00
  • @Verarind to what 32k are you refering? (don't see that anywhere and his post hasn't been edited) – Thomas Oct 19 '15 at 12:04
  • Take a look at the ["Pigeonhole Principle"](https://en.wikipedia.org/wiki/Pigeonhole_principle). There are 2^32 possible hash codes, and an infinite number of possible types. – dcastro Oct 19 '15 at 12:05
  • @Thomas SergeyBerezovskiy changed his comment. I quoted the text part that contains the 32k you expect. – Sebastian Schumann Oct 19 '15 at 12:12

3 Answers3

3

I know that GetHashCode() by itself, does not guarantee uniqueness between any two different Objects, but I'm interested does that apply for any two Types as well

A Type is an object so the same applies

is there a chance that: typeof(MyClass1).GetHashCode() == typeof(MyGrandchild1).GetHashCode() will return true

Yes

if there's a chance for (1): is there a chance that typeof(MyClass1) == typeof(MyGrandchild1) will return true

No, they are different types

worst case scenario: is there a chance that typeof(int) == typeof(long)will return true

No, for the same reason as above.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

(1) Yes there is a chance that typeof(MyClass1).GetHashCode() == typeof(MyGrandchild1).GetHashCode() would be true. This should make sense as an int has a lesser number of possible values that all the possible class names that you could define. There would mathematically have to be collisions.

(2) No, because System.Type has a properly implemented Equals method. The purpose of the Equals method is not only indicate equality, but to "break the tie" when GetHashCode() produces a collision.

(3) No. See (2).

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • what about **(4)** `typeof(int).GetHashCode() == typeof(long).GetHashCode()`, is there a _chance_ for that to return `true`? – avance70 Oct 20 '15 at 07:14
  • 1
    @avance70 - Yes, but very unlikely. It would possibly be a breaking change for someone's code so Microsoft wouldn't do it. – Enigmativity Oct 20 '15 at 08:28
  • 1
    `Type` doesn't define any special `GetHashCode` implementation. It uses `object.GetHashCode()`, which returns a pseudo-random number when it's first called on each instance. So it is very likely that sooner or later these 2 hash codes will be the same. – Jakub Lortz Oct 20 '15 at 21:36
  • @JakubLortz - That's very interesting. I guess that would work just fine as `Equals` would handle the rare tie-breaker cases. – Enigmativity Oct 21 '15 at 00:12
1
  1. GetHashCode returns an integer, so there is a limit of unique values it can return. There is no limit of defined types, so yes, there is a chance that typeof(MyClass1).GetHashCode() == typeof(MyGrandchild1).GetHashCode() will return true.

2,3. Hash code is never used to check equality. The only relation between hash code and equality is that equal objects should have the same hash code.

Edit

One more answer, plus a some explanations.

  1. Types implement reference equality. The CLR makes sure the instances are unique

A Type object that represents a type is unique; that is, two Type object references refer to the same object if and only if they represent the same type. This allows for comparison of Type objects using reference equality.

It means that Type can use (and it does) the standard object implementation of GetHashCode. This implementation returns a pseudo-random number when it's first called on each instance.

So asking if typeof(int).GetHashCode() can be equal to typeof(long).GetHashCode() is basically asking if two pseudo-random numbers can be equal. Yes they can.

If you want more details about object.GetHashCode() implementation, read this blog post

Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
  • isn't it used in any kind of a hash table, such as Dictionary ? – avance70 Oct 19 '15 at 12:10
  • 1
    @avance70 It's used to assign items to buckets. If you implement `GetHashCode()` as `return 1` in `MyClass`, `Dictionary` will still work correctly, but all items will be stored in a single bucket. Only performance will suffer, as retrieving items will now be O(n) instead of close to O(1). – Jakub Lortz Oct 19 '15 at 12:28
  • thanks! and what about **(4)** `typeof(int).GetHashCode() == typeof(long).GetHashCode()`, is there a _chance_ for that to return `true`? – avance70 Oct 20 '15 at 07:14
  • 1
    @avance70 I just added a lot more details to my answer. – Jakub Lortz Oct 20 '15 at 21:27