public struct DictStruct
{
public int x;
public DictStruct(int x)
{
this.x = x;
}
}
public class Test : MonoBehaviour
{
private void Update()
{
TestEnumDict();
}
private void TestStruct()
{
Dictionary<DictStruct, int> dict = new Dictionary<DictStruct, int>();
dict.Add(new DictStruct(1), 1);
}
}
这时有20B的GC,其中int=4字节,而在比较的时候因为ObjectEqualityComparer的GetHashCode调用的是object的GetHashCode,需要转换成object进行比较。
internal class ObjectEqualityComparer<T>: EqualityComparer<T>
{
[Pure]
public override bool Equals(T x, T y) {
if (x != null) {
if (y != null) return x.Equals(y);
return false;
}
if (y != null) return false;
retur