如何实现用作字典键的结构

问题描述 投票:1回答:1

我正在使用自定义结构作为字典中的键:


    public struct TV
    {
        public int B;
        public int BC;
        public int P;
        public int PO;
        public int PC;
        public int W;
        public int WO;
        public int WC;
        public int R;
        public int RO;
        public int RC;
        public int G;
        public int GO;
        public int GC;
        public int GW;
        public int GWO;
        public int GWC;

        public TV(int b, int bC, int p, int po, int pC, int w, int wo, int wC, int r, int ro, int rC, int g, int go, int gC, int gw, int gwo, int gwC)
        {
            B = b;
            BC = bC;
            P = p;
            PO = po;
            PC = pC;
            W = w;
            WO = wo;
            WC = wC;
            R = r;
            RO = ro;
            RC = rC;
            G = g;
            GO = go;
            GC = gC;
            GW = gw;
            GWO = gwo;
            GWC = gwC;
    }

    }

但是,当我使用.containskey和getkey时,我却得到了疯狂的垃圾回收分配,每帧数以百万计]]

我已经研究了这个问题,并且我知道这与结构的错误装箱有关,这是因为未实现IEquatable并覆盖了equals()和getHashCode之类的某些方法。

我已经看到了一些实现这些示例的示例,但是我只发现了包含2个或3个变量的小型结构的示例,因为我的结构具有17个值,所以我不知道该如何实现它,因为我不理解哈希码如何工作,如果有人可以引导我朝正确的方向前进,我将不胜感激,我应该在此结构中添加哪些内容以使其可用作字典键?

我正在使用自定义结构作为字典中的键:public struct TV {public int B;公元前公元前公共int P;公共int PO;公用int PC; ...

c# dictionary unity3d struct
1个回答
4
投票

这里的关键是:

© www.soinside.com 2019 - 2024. All rights reserved.