我尝试检查 null 但编译器警告这种情况永远不会发生。我应该寻找什么?
假设您想获取键的值does存在、使用Dictionary<TKey, TValue>.TryGetValue https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.trygetvalue:
int value;
if (dictionary.TryGetValue(key, out value))
{
// Key was in dictionary; "value" contains corresponding value
}
else
{
// Key wasn't in dictionary; "value" is now 0
}
(Using ContainsKey
然后索引器让它看起来两次,这是毫无意义的。)
请注意,即使您were使用引用类型,检查 null 不起作用 - 索引器Dictionary<,>
如果您请求丢失的密钥,则会抛出异常,而不是返回 null。 (这是一个很大的区别Dictionary<,>
and Hashtable
.)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)