【发布时间】:2021-01-23 01:10:45
【问题描述】:
我正在尝试使用以下代码通过它们的一个属性(.Transaction.topLeftX,一个整数)对两个对象进行排序,以创建一个在 Sort 方法中使用的比较器:
public class RespComp : IComparer<Kairos.Net.RecognizeImage>
{
public Kairos.Net.RecognizeImage Compare(Kairos.Net.RecognizeImage x, Kairos.Net.RecognizeImage y)
{
if (x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX) <= 0) return x;
else return y;
}
}
但是,我收到错误消息 Error CS0738 'RecogniseFacesKairos.RespComp' 没有实现接口成员 'IComparer.Compare(RecognizeImage, RecognizeImage)'。 “RecogniseFacesKairos.RespComp.Compare(RecognizeImage, RecognizeImage)”无法实现“IComparer.Compare(RecognizeImage, RecognizeImage)”,因为它没有匹配的返回类型“int”。
Sort方法中使用的比较器是否需要返回类型为int?
【问题讨论】:
-
可以把方法的签名贴在接口里吗?
-
"因为它没有匹配的返回类型'int'"。是的,您的方法必须返回“int”。
-
because it does not have the matching return type of 'int'.是的 - 这就是你需要做的。