How to perform a binary search on IList?

前端 未结 11 1376
执念已碎
执念已碎 2020-11-28 09:32

Simple question - given an IList how do you perform a binary search without writing the method yourself and without copying the data to a type with bui

11条回答
  •  庸人自扰
    2020-11-28 10:25

    You are going to have a couple of problems binary-searching an IList, First ,like you mentioned, the BinarySearch method on the List is not a member of the IList interface. Second, you have no way of sorting the list prior to searching (which you must do for a binary search to work).

    I think your best bet is to create a new List, sort it, and then search. It's not perfect but you don't have to many options if you have an IList.

提交回复
热议问题