未经处理的异常: system.invalidoperationexception: 未能比较数组中的两个元素。 ---> system.argumentexception: 必须至少有 一个对象实现 icomparable。在 system.collections.comparer.compare(object a, object b)在 system.array.sorterobjectarray.insertionsort(int32 lo, int32 hi)在 system.array.sorterobjectarray.introsort(int32 lo, int32 hi, int32 depthlimit)在 system.array.sorterobjectarray.introspectivesort(int32 left, int32 length)--- 内部异常堆栈跟踪的结尾 ---在 system.array.sorterobjectarray.introspectivesort(int32 left, int32 length)在 system.array.sort(array keys, array items, int32 index, int32 length, icomparer comparer)在 system.collections.arraylist.sort(int32 index, int32 count, icomparer comparer)在 system.collections.arraylist.sort()在 homework.program.main(string[] args) 位置 e:\code\programs\c#\homework\program.cs:行号 201
arraylist 类(system.collections.arraylist):https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.arraylist?view=netframework-4.8
icomparable 接口:https://docs.microsoft.com/zh-cn/dotnet/api/system.icomparable?view=netframework-4.8
icomparable 接口:https://docs.microsoft.com/zh-cn/dotnet/api/system.icomparable-1?view=netframework-4.8
icomparable 接口不适用于arraylist。
arraylist.sort()方法只能对实现icomparable接口的类对象进行排序。
使用非泛型的comparable接口替换泛型的comparable 接口。
class circle : ellipse, icomparable
public int compareto(object other){return this.radius.compareto(((circle)other).radius);}
https://blog.csdn.net/weixin_43272781/article/details/105292114
https://blog.csdn.net/maybe_ch/article/details/81359408
总结
以上是凯发k8官方网为你收集整理的c#——实现icomparablet 接口,arraylist调用arraylist.sort()抛出system.invalidoperationexception异常凯发k8官方网的解决方案的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。