- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个名为 Shape
的对象,它包含一个 public int[,] coordinate { get;放;
字段。
我有一个单独的类,它有一个 Shape
对象的集合。在特定点,我想检查:
if(shapes.Contains(shape))
{
// DoSomething
}
所以在 Shape
类中,我添加了 IComparable
引用并插入了 CompareTo
方法:
public int CompareTo(Shape other)
{
return this.coordinate.Equals(other.coordinate);
}
但是我收到一个错误:
Cannot implicitly convert type 'bool' to 'int'
因此,我该如何表述返回,以便它返回一个 int 而不是像现在这样返回一个 bool?
更新
如果我将返回代码更改为:
return this.coordinate.CompareTo(other.coordinate);
我收到以下错误消息:
Error 1 'ShapeD.Game_Objects.Shape' does not implement interface member 'System.IComparable.CompareTo(ShapeD.Game_Objects.Shape)'. 'ShapeD.Game_Objects.Shape.CompareTo(ShapeD.Game_Objects.Shape)' cannot implement 'System.IComparable.CompareTo(ShapeD.Game_Objects.Shape)' because it does not have the matching return type of 'int'. C:\Users\Usmaan\Documents\Visual Studio 2012\Projects\ShapeD\ShapeD\ShapeD\Game Objects\Shape.cs 10 18 ShapeD
最佳答案
IComparable 意味着,在某种意义上可以比较两个对象,您可以判断哪个对象具有“更高的值(value)”。它通常用于排序目的。您应该改写 Equals
方法。您还应该使用 Point 结构而不是数组。
class Shape : IEquatable<Shape>
{
public Point coordinate { get; set; }
public bool Equals(Shape other)
{
if (other == null) return false;
return coordinate.Equals(other.coordinate);
}
public override bool Equals(object other)
{
if (other == null) return false;
if (ReferenceEquals(this, other)) return true;
var shape = other as Shape;
return Equals(shape);
}
public override int GetHashCode()
{
return coordinate.GetHashCode()
}
}
关于c# - C# 中的 IComparable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18072425/
我如何实现 IComparer int CompareTo(object obj) 有一个额外的参数,比如 int CompareTo(object obj, Dictionary preferenc
我在正确使用 IComparable<> 时遇到了一些麻烦界面。我还创建了一个实现 IComparer<> 的类.我几乎从一本书中复制了确切的编码示例,但对其进行了重新设计以适应我的情况,但它似乎无法
我是否应该同时实现 IComparable和通用 IComparable ?如果我只实现其中之一,是否有任何限制? 最佳答案 是的,您应该同时实现两者。 如果你实现其中一个,任何依赖于另一个的代码都会
这个问题在这里已经有了答案: When to use IComparable Vs. IComparer (8 个答案) 关闭 4 年前。 IComparable 和IComparer 接口(int
我正在尝试实现此链接的相同示例,但更注重受抚养 child 的数量。 http://www.codeproject.com/Articles/42839/Sorting-Lists-using-ICo
在大多数地方,我读到从 IComparable 继承是个好主意。和 IComparable在你的类中提供与非通用集合的兼容性。我的问题是为什么IComparable不继承自 IComparable那么
我正在使用 WPF 博士的 ObservableSortedDictionary。 构造函数如下所示: public ObservableSortedDictionary(IComparer comp
我知道 IComparable 之间有很大的区别和 IComparable一般情况下,请参阅 this , 但在这种搜索方法中,它不会有任何区别,还是会这样? public static int Se
我是 C# 的新手(6 个月的工作经验),但它看起来与 Java 非常相似,所以我感觉很自在。 但是,今天我尝试实现 IComparer 接口(interface)并想知道为什么它会给我一个错误: p
我有 Employee 类,我需要实现 IComparable 并使用 CompareTo 方法按姓名对员工进行排序。据我所知,我必须返回 1、-1 和 0,但我该如何使用这些字符串? 这是我的。 c
是否可以使用 iComparer 使用对象中的两个值对列表进行排序? 我有一个基于 value1 排序的自定义比较器类。但是对 value1 和 value2 进行排序的最佳方法是什么? 按 valu
我为装箱的 RegistryItem 对象定义了以下 IComparer: public class BoxedRegistryItemComparer : IComparer { publi
我正在尝试在相似值列表中获取唯一值,仅通过管道分隔字符串中的一个元素来区分...我不断得到至少一个对象必须实现 Icomparable。我不明白为什么我总是得到这个。我能够按该值进行分组...为什么我
假设我们有这 3 个类: Class BaseClass : System.IComparable { [int] $Value BaseClass([int] $v) {
所以我在这个错误上画了一个空白。无法比较数组中的两个元素。Array.Sort(患者);是产生错误的地方。我确实有一个 IComparable 接口(interface)和一个包含以下代码的类文件:T
我刚开始尝试使用 IEnumerable 接口(interface)。我总是只编写自定义哈希排序而不是尝试使用 native 语法,因为我对实现有些困惑。我正在尝试确定是否可以使用 BinarySea
我有一个 MyClass 类,我想像这样把它作为字典的键: Dictionary dict = new Dictionary(); 我想确保 MyClass 是唯一键,唯一性是通过查看 MyClass
在下面的代码中,我以 .NET 2.0 Framework 为目标。 我可以将 Programmer(派生)对象传递给需要 Person(基类)的 Compare 方法 但由于程序员是一个人(简单的
我正在尝试使用 IComparer 对对象数组进行排序。 我编写了代码,但它仅适用于特定对象。例如: 这门课 public class Cars { public string Name {
如何实现需要参数的 IComparer(可能不相关,但我在 Linq 查询中使用它)? 我想它应该这样称呼: ListOfObjectsToSort.orderBy(x => x, myCustomC
我是一名优秀的程序员,十分优秀!