在我的课堂上,我已经实现了Equals
和GetHashCode
. 然而,当我在我的 C# 代码中将它用作字典的键时,我得到了错误:"Key not found exception"
谢谢,
public class Time: IEquatable<Time>
{
public String hour;
public String minute;
public Time()
{
hour = "00";
minute = "00";
}
public Time(String hour, String minute)
: this()
{
this.hour = hour;
this.minute = minute;
}
public override int GetHashCode()
{
int hash = int.Parse(hour) * 60 + int.Parse(minute);
return hash.GetHashCode();
}
public override bool Equals(Time time)
{
return (this.hour == time.hour && this.minute == time.minute);
}
}
我使用它的代码:
Dictionary<Time, int> time2RowIndex = new Dictionary<Time, int>();
...
int beginRow = 0;
if(time2RowIndex.ContainsKey(time.hour))
beginRow = time2RowIndex [time.hour];