日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#——实现IComparableT 接口,ArrayLIst调用ArrayLIst.Sort()抛出System.InvalidOperationException异常解决方案

發(fā)布時(shí)間:2024/10/5 C# 90 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#——实现IComparableT 接口,ArrayLIst调用ArrayLIst.Sort()抛出System.InvalidOperationException异常解决方案 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問題描述

未經(jīng)處理的異常: System.InvalidOperationException: 未能比較數(shù)組中的兩個(gè)元素。 ---> System.ArgumentException: 必須至少有 一個(gè)對象實(shí)現(xiàn) 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)--- 內(nèi)部異常堆棧跟蹤的結(jié)尾 ---在 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<T> 接口:https://docs.microsoft.com/zh-cn/dotnet/api/system.icomparable-1?view=netframework-4.8

問題分析

IComparable<T> 接口不適用于ArrayLIst。

ArrayLIst.Sort()方法只能對實(shí)現(xiàn)IComparable接口的類對象進(jìn)行排序。

解決方案

使用非泛型的Comparable接口替換泛型的Comparable<T> 接口。

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

總結(jié)

以上是生活随笔為你收集整理的C#——实现IComparableT 接口,ArrayLIst调用ArrayLIst.Sort()抛出System.InvalidOperationException异常解决方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。