In C# both the System.Generic.List
class and the System.Array
class already haveThe best way to implement a public generic method for binary search is by invoking the BinarySearch()
methods which work with Comparer
delegates, or with objects implementingmethod of IComparableSystem.Array
. They are known to work very well, and they perform actual search, meaning that they return the index of the item found, (or, if not found, the negated index at which it should be inserted,) instead of what your methods do, which is to simply answer the question of whether an item exists in an array.
SoBetter yet, why are you trying to re-invent this wheel?
(I would have added this asdon't implement such a comment rather than as an answermethod at all, but I see no 'comment' linkand call System.Array.BinarySearch()
directly.)