Hey there, I just encounter something that feels like a bug.
Let’s say we have an empty struct :
public struct EmptyStruct
{
}
If we create two instances of this struct and store them in two different variables :
var emptyStruct1 = new EmptyStruct();
var emptyStruct2 = new EmptyStruct();
I would expect .Equals to be true between them and thus .GetHashCode() returning the same value :
Debug.Log($"Are Empty StructGetHashCode the same ? {emptyStruct1.GetHashCode() == emptyStruct2.GetHashCode()}");
Debug.Log($"Are EmptyStruct Equals ? {emptyStruct1.Equals(emptyStruct2)}");
It turns out everything is fine in Editor and in Mono Build.
Are EmptyStruct GetHashCode the same ? True
Are EmptyStruct Equals ? True
But …
When I build the project with IL2CPP, .Equals() still is true, but their .GetHashCode() returns different values, which seems contradictory to the classic Equals/GetHashCode usage.
Are EmptyStruct GetHashCode the same ? False
Are EmptyStruct Equals ? True
This behaviour is observed in Unity 2020.3.28f1 and also Unity 2021.2.15f1 on Android IL2CPP builds (and I think in all IL2CPP builds).
Ugly ways to ‘fix’ this involve overriding .GetHashCode() in the empty struct or just adding an unused field.
Is there something I miss ?
Just in case I created a bug report (https://fogbugz.unity3d.com/default.asp?1411455_rs5538034dq74a9l) and a colleague of mine recommended me to tag @JoshPeterson
Thank you all for your insights