繁体   English   中英

字典 ContainsKey 方法

[英]dictionary ContainsKey method

请解释为什么字典的“getAt”方法失败

List<BString> infoKeys = new List<BString>(infoDict.Keys); 
if (infoKeys.Contains(TorrentFileKeyWords.FILES_KEY) == true) //"files"
{   
        List<BaseType> multiFiles = ((BList)dict[TorrentFileKeyWords.FILES_KEY]).Value; <<< this fails

所以 infoDict 是一个Dictionary<String, BString> Contains on infoDict.Keys 用于查找特定项目(BString 类型)但是第 4 行失败......没有感觉

我没有使用 c#.. 所以我必须覆盖哪些方法(现在我有:GetHashCode、==、!= & equals)

您不需要将Keys复制到新列表即可执行查找。 事实上,您可以使用TryGetValue方法检查键是否存在于字典中在单个操作中检索其关联值:

BList bList;
if (dict.TryGetValue(TorrentFileKeyWords.FILES_KEY, out bList))
{
    List<BaseType> multiFiles = bList.Value;
    // use multiFiles here
}

我怀疑问题是你在一个地方使用infoDict ,而在另一个地方使用dict ...

目前尚不清楚为什么要从infoDict的键创建列表,而不是仅仅调用ContainsKey或(更好)使用TryGetValue开始。 此外,我建议不要为您的类型名称添加“B”前缀。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM