[英]dictionary ContainsKey method
Please explain why dictionary's 'getAt' method fails请解释为什么字典的“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
So infoDict is a Dictionary<String, BString>
Contains on infoDict.Keys is used to find a specific item (of type BString) But line 4 fails... doesnt have sens所以 infoDict 是一个
Dictionary<String, BString>
Contains on infoDict.Keys 用于查找特定项目(BString 类型)但是第 4 行失败......没有感觉
I am not used with c#.. so what methods do I have to override (now i have: GetHashCode, ==, != & equals)我没有使用 c#.. 所以我必须覆盖哪些方法(现在我有:GetHashCode、==、!= & equals)
You shouldn't need to copy your Keys
to a new list to perform the lookup.您不需要将
Keys
复制到新列表即可执行查找。 In fact, you can check whether the key is present in the dictionary and retrieve its associated value in a single operation using the TryGetValue
method:事实上,您可以使用
TryGetValue
方法检查键是否存在于字典中并在单个操作中检索其关联值:
BList bList;
if (dict.TryGetValue(TorrentFileKeyWords.FILES_KEY, out bList))
{
List<BaseType> multiFiles = bList.Value;
// use multiFiles here
}
I suspect the problem is that you're using infoDict
in one place, and dict
in another...我怀疑问题是你在一个地方使用
infoDict
,而在另一个地方使用dict
...
It's not clear why you're creating a list from the keys of infoDict
rather than just calling ContainsKey
, or (better) using TryGetValue
to start with.目前尚不清楚为什么要从
infoDict
的键创建列表,而不是仅仅调用ContainsKey
或(更好)使用TryGetValue
开始。 Additionally, I would advise against a "B" prefix for your type names.此外,我建议不要为您的类型名称添加“B”前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.