Inaccessible BinarySearch, or how the CLR is as dumb as can be RRS feed

  • General discussion

  • Why is BinarySearch bound to an array? 

    Given a function that evaluates at integer points, why do I have to construct an array of values to find zero?

    Why can List <T> .BinarySearch only look for T?

    I have List < int > and I want to look for a string using a custom comparator.  I can search for a string if I use ArrayList and a custom comparator but then all my integers will be boxed, which is a performance hit.

    No, I cannot convert the string to an integer up front, as it is equivalent to the problem I am trying to solve.

    No, I cannot use a list of strings instead; that would be linear in the size of the list and therefore equivalent to using Find.

    So it seems I cannot use CRL’s BinarySearch at all and I need to provide my own implementation.  How dumb.

    Christopher Yeleighton

    • Changed type Fred BaoModerator Monday, September 8, 2014 8:10 AM The discussion type is more suitable for this case
    Sunday, August 31, 2014 7:35 PM

All replies

  • Can you explain why you can't convert the string to an intetger.  An integer ssearch is much more efficient than a string search.  If you have leading zeroes in your string integer then you really have a algorithm issue and not a coding issue.  Number should be unique and not depend on leading zeroes.  The leading zeroes is only visual requirement for text.


    Sunday, August 31, 2014 8:43 PM
  • Because the indices are handles and I need to find a handle by content.

    Christopher Yeleighton

    Wednesday, September 3, 2014 6:27 AM
  • BinarySearch is so rarely used in .NET that the best answer that can be provided here is simply "because nobody cares about BinarySearch".
    Wednesday, September 3, 2014 6:48 AM