由于极其可怕的原因,我的雇主申请中有这个结构。
我试图覆盖相等运算符,但出现错误Error 9 Operator '==' cannot be applied to operands of type 'TR_St_DateTime' and 'TR_St_DateTime'
。
我错过了什么?
public struct TR_St_DateTime : IEquatable<TR_St_DateTime>
{
public int Year;
public int Month;
public int Day;
public int Hour;
public int Minute;
public int Second;
public TR_St_DateTime(DateTime time)
{
Day = time.Day;
Hour = time.Hour;
Minute = time.Minute;
Second = time.Second;
Month = time.Month;
Year = time.Year;
}
public override bool Equals(object obj)
{
TR_St_DateTime o = (TR_St_DateTime) obj;
return Equals(o);
}
public override int GetHashCode()
{
return Year ^ Month ^ Day ^ Hour ^ Minute ^ Second;
}
public override string ToString()
{
return String.Format("{0}/{1}/{2}", Day, Month, Year);
}
public bool Equals(TR_St_DateTime other)
{
return ((Day == other.Day) && (Month == other.Month) && (Year == other.Year) && (Minute == other.Minute) && (Hour == other.Hour) && (Second == other.Second));
}
}
更新:这似乎==
不起作用,但Equals
确实如此。
无需Equals
在结构上实现。