使用先前从密钥集合中检索到的密钥时出现 KeyNotFoundException?
我有以下代码,由于某种原因,即使我使用的是我在上面检索到的几行的密钥,我也会收到 KeyNotFoundException 。有谁知道这不起作用的情况吗?我很困惑。顺便说一句,'SchemaElementType 是一个枚举。
public class DefaultValue
{
private Dictionary<Parameter, string> _params;
public DefaultValue(Dictionary<Parameter, string> parameters)
{
_params = parameters;
}
public string GetParameterValue(string name)
{
foreach(Parameter param in _params.Keys)
{
if(param.ParamName.Equals(name))
{
// **** Issue here ****
return _params[param];
}
}
return string.Empty;
}
}
[DataContract]
public class Parameter
{
#region Members
private Guid _guid;
private Guid _formulaGuid;
private string _name;
#endregion
#region Constructor
public Parameter(Guid guid, Guid formulaGuid, string name, SchemaElementType type)
{
ParamGuid = guid;
FormulaGuid = formulaGuid;
ParamName = name;
ParamType = type;
}
public Parameter()
{}
#endregion
#region Properties
[DataMember]
public Guid ParamGuid
{
get { return _guid; }
set { _guid = value; }
}
[DataMember]
public Guid FormulaGuid
{
get { return _formulaGuid; }
set { _formulaGuid = value; }
}
[DataMember]
public string ParamName
{
get { return _name; }
set { _name = value; }
}
[DataMember]
public SchemaElementType ParamType { get; set; }
#endregion
#region Overrides
public bool Equals(Parameter other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
bool result =other._guid.Equals(_guid);
result = result && other._formulaGuid.Equals(_formulaGuid);
result = result && Equals(other._name, _name);
result = result && Equals(other.ParamType, ParamType);
return result;
}
public override int GetHashCode()
{
unchecked
{
int result = _guid.GetHashCode();
result = (result*397) ^ _formulaGuid.GetHashCode();
result = (result*397) ^ (_name != null ? _name.GetHashCode() : 0);
result = (result*397) ^ ParamType.GetHashCode();
return result;
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Parameter)) return false;
return Equals((Parameter) obj);
}
#endregion
}
I've got the following code where for some reason I'm getting a KeyNotFoundException even though I'm using a key that I had retrived a couple of lines above. Does anyone know of a situation where this wouldn't work? I'm stumped. BTW 'SchemaElementType is an enum.
public class DefaultValue
{
private Dictionary<Parameter, string> _params;
public DefaultValue(Dictionary<Parameter, string> parameters)
{
_params = parameters;
}
public string GetParameterValue(string name)
{
foreach(Parameter param in _params.Keys)
{
if(param.ParamName.Equals(name))
{
// **** Issue here ****
return _params[param];
}
}
return string.Empty;
}
}
[DataContract]
public class Parameter
{
#region Members
private Guid _guid;
private Guid _formulaGuid;
private string _name;
#endregion
#region Constructor
public Parameter(Guid guid, Guid formulaGuid, string name, SchemaElementType type)
{
ParamGuid = guid;
FormulaGuid = formulaGuid;
ParamName = name;
ParamType = type;
}
public Parameter()
{}
#endregion
#region Properties
[DataMember]
public Guid ParamGuid
{
get { return _guid; }
set { _guid = value; }
}
[DataMember]
public Guid FormulaGuid
{
get { return _formulaGuid; }
set { _formulaGuid = value; }
}
[DataMember]
public string ParamName
{
get { return _name; }
set { _name = value; }
}
[DataMember]
public SchemaElementType ParamType { get; set; }
#endregion
#region Overrides
public bool Equals(Parameter other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
bool result =other._guid.Equals(_guid);
result = result && other._formulaGuid.Equals(_formulaGuid);
result = result && Equals(other._name, _name);
result = result && Equals(other.ParamType, ParamType);
return result;
}
public override int GetHashCode()
{
unchecked
{
int result = _guid.GetHashCode();
result = (result*397) ^ _formulaGuid.GetHashCode();
result = (result*397) ^ (_name != null ? _name.GetHashCode() : 0);
result = (result*397) ^ ParamType.GetHashCode();
return result;
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Parameter)) return false;
return Equals((Parameter) obj);
}
#endregion
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我担心
Parameter
是可变的。如果(将其添加到字典后)您更改了生成GetHashCode()
时使用的任何值(即所有值),则所有投注均无效您不保证会再次看到您的商品。我不会创建这些公共 setter,即Actaully,我可能会放弃显式字段并使用 C# 3.0 自动实现的属性:
作为通过更改参数来中断的示例:
作为最后的想法;如果典型的用法是通过
string
查找,那么为什么不使用名称作为内部字典的键呢?目前你还没有正确使用字典。I worry about the fact that
Parameter
is mutable. If (after adding it to the dictionary) you have changed any of the values that are used when generatingGetHashCode()
(i.e. all of them) then all bets are off and you are not guaranteed to see your item again. I would not make these public setters, i.e.Actaully, I'd probably drop the explicit fields and use C# 3.0 automatically implemented properties:
As an example that breaks by changing the parameter:
As a final thought; if the typical usage is to lookup by
string
, then why not use the name as the key for the internal dictionary? At the moment you aren't using the dictionary properly.严格来说,您没有检索上面几行的(查找键),而是检索在某个时刻用于计算哈希键的对象。
当您插入字典时,将调用键对象的 GetHashKey 方法。如果从插入键值对到执行代码的时间发生变化,您将得到所描述的行为。 (除非 GetHashKey no 返回一个与不同键值对的键匹配的值,在这种情况下,您会得到非常奇怪的行为而不是例外)
我会在插入和检索时查找哈希键的值看看它们之间是否存在不匹配
Well strictly speaking you're not retrieving the (lookup key) a few lines above you are retreiving an object that at some point was used for calculating the hash key.
When you insert into a Dictionary the GetHashKey method of the key object will be called. If that changes from the time you insert the key-value pair to the time your code is executed you will get the described behaviour. (unless of cause if the GetHashKey no returns a value matching a key to a different key-value pair, in that case you get really weird behaviour not an exception)
I'd look for the value of the hash key when inserting and when retreiving and see if there's a mismatch between them
您可以利用 KeyValuePair<>班级:
You can take advantage of the KeyValuePair<> class: