gpt4 book ai didi

c# - 在字典中创建自定义键

转载 作者:行者123 更新时间:2023-11-30 20:46:51 27 4
gpt4 key购买 nike

<分区>

我是 C# 的新手,我正在尝试为字典创建自定义键。

下面是我创建类的方法

public class ImageKey
{
public int RId;
public int EId;
public int LId;

public ImageKey(int rId, int eId, int lId)
{
RId = rId;
EId = eId;
LId = lId;
}


public bool Equals(ImageKey x, ImageKey y)
{
return x.RId == y.RId && x.LId == y.LId &&
x.EId == y.EId;
}

public int GetHashCode(ImageKey obj)
{
int hash = 13;
hash = (hash * 7) + obj.RId.GetHashCode();
hash = (hash * 7) + obj.EId.GetHashCode();
hash = (hash * 7) + obj.LId.GetHashCode();
return hash;
}

}

我把它用作那样的哈希集

HashSet<ImageKey> resourcesId = new HashSet<ImageKey>();

作为字典

var enteries = new Dictionary<ImageKey, int>();

但是当我遍历并尝试查看是否有匹配的键时,此语句对 ContainsKey 失败

ImageKey key = new ImageKey(rId, eId, lId);
bool knownId = enteries.ContainsKey(key);
if (!knownId)
{
enteries.Add(key, new IgaEntry());
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com