This method searches for a value in a one-dimensional sorted array using a specified IComparer interface.
public static int BinarySearch(Array arr, Object val, IComparer comparer)
- arr : The one-dimensional sorted array in which the search will happen.
- val : The object value which is to search for.
- comparer : When comparing elements then the IComparer implementation is used.
Return Value: It returns the index of the specified value in the specified array if the value is found otherwise it returns a negative number. There are different cases of return values as follows:
- If the value is not found and value is less than one or more elements in the array, the negative number returned is the bitwise complement of the index of the first element that is larger than value.
- If the value is not found and value is greater than all elements in the array, the negative number returned is the bitwise complement of (the index of the last element plus 1).
- If this method is called with a non-sorted array, the return value can be incorrect and a negative number could be returned, even if the value is present in the array.
- ArgumentNullException: If the array is null.
- RankException: If array is multidimensional.
- ArgumentException: If the range is less than lower bound OR length is less than 0.
- ArgumentException: If the comparer is null, and value is of a type that is not compatible with the elements of array.
- InvalidOperationException: If the comparer is null, value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
Example 1: In this example, the array stores some string value and find some string value after sorting the array.
The original Array ABCD IJKL XYZ EFGH MNOP sorted array ABCD EFGH IJKL MNOP XYZ 1st call The object EFGH is at index 1 2nd call The object ABCD is at index 0
The original Array 20 10 30 40 50 sorted array 10 20 30 40 50 1st call The object 10 is at index 0 2nd call The object 60 is not found Next larger object is at index 5
- C# | Array.BinarySearch(Array, Int32, Int32, Object, IComparer) Method
- C# | Check if an array object is equal to another array object
- Object toString() Method in Java
- C# | Method returning an object
- Array.BinarySearch(Array, Object) Method with examples in C#
- C# | Convert.GetTypeCode(Object) Method
- C# | Object.GetHashCode() Method with Examples
- C# | Object.GetTypeCode() Method with Examples
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- C# | Byte.CompareTo(Object) Method
- C# | Byte.Equals(Object) Method
- Object.ReferenceEquals() Method in C#
- C# | Uri.Equals(Object) Method
- C# | Boolean.Equals(Object) Method
- C# | Boolean.CompareTo(Object) Method
- Object.MemberwiseClone Method in C# with Examples
- C# | Remove the first occurrence of a specific object from the ArrayList
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- C# | Class and Object
- Different ways to create an Object in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.