我有一個叫做Shape
的對象,其中包含一個public int[,] coordinate { get; set; }
字段。C在C中可以比較#
我有一個單獨的類,其中有一個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,因爲此刻它正在這樣做?
UPDATE
如果我改變返回代碼:
return this.coordinate.CompareTo(other.coordinate);
我收到以下錯誤mesage:
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
新的錯誤信息是公平的簡單來解決,更改** public int CompareTo(Shape其他)** to ** public int CompareTo(object other)**但是然後您將面臨投射問題並且CompareTo對於多維數組不存在。 –