参考我之前问的问题: 比较两个包含很多对象的列表
看到通过实现 IEqualityComparer 接口进行比较的速度令人印象深刻:这里的示例
正如我在另一个问题中提到的那样,这种比较有助于我在目标文件夹上备份源文件夹。知道我想同步到文件夹,因此我需要比较文件的日期。每当我做类似的事情时:
public class MyFileComparer2 : IEqualityComparer<MyFile>
{
public bool Equals(MyFile s, MyFile d)
{
return
s.compareName.Equals(d.compareName) &&
s.size == d.size &&
s.deepness == d.deepness &&
s.dateModified.Date <= d.dateModified.Date; // This line does not work.
// I also tried comparing the strings by converting it to a string and it does
// not work. It does not give me an error but it does not seem to include the files
// where s.dateModified.Date < d.dateModified.Date
}
public int GetHashCode(MyFile a)
{
int rt = (a.compareName.GetHashCode() * 251 + a.size.GetHashCode() * 251 + a.deepness.GetHashCode() + a.dateModified.Date.GetHashCode());
return rt;
}
}
如果我可以使用大于或等于符号做类似的事情,那就太好了。我也尝试使用 tick 属性,但它不起作用。也许我做错了什么。我相信不可能将事物与实现此接口的小于等号进行比较。此外,我不明白这个类是如何工作的;我只知道它遍历整个列表的速度令人印象深刻。