I have array with int values
int[] myArr = (int[]) alItems.ToArray(typeof (int));
index = Array.BinarySearch(myArr, searchNumber);
When I have searchNumber in myArr it give me negative index, why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If binary search returns -1, then the value is not within the array. This can also occur if the array is not sorted (which it needs to be for binary search to work).
Before using binary search you should understand how it works. Take some time to do just a little bit of research before you put it in your code.