Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Array.BinarySearch Method
DefinitionSearches a one-dimensional sorted Array for a value, using a binary search algorithm. In this articleOverloads
BinarySearch(Array, Object)Searches an entire one-dimensional sorted array for a specific element, using the IComparable interface implemented by each element of the array and by the specified object.
Parametersarray Array The sorted one-dimensional Array to search. value Object The object to search for. ReturnsInt32The index of the specified Exceptions
ExamplesThe following code example shows how to use BinarySearch to locate a specific object in an Array. Note The array is created with its elements in ascending sort order. The BinarySearch method requires the array to be sorted in ascending order.
RemarksThis method does not support searching arrays that contain negative indexes. If the
Array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~ in C#, Either Note If Duplicate elements are allowed. If the
Array contains more than one element equal to
Note For every element tested, This method is an O(log See also
Applies toBinarySearch(Array, Object, IComparer)Searches an entire one-dimensional sorted array for a value using the specified IComparer interface.
Parametersarray Array The sorted one-dimensional Array to search. value Object The object to search for. ReturnsInt32The index of the
specified Exceptions
RemarksThis method does not support searching arrays that contain negative indexes. If the Array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~ in C#, The comparer customizes how the elements are compared. For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches. If If Note If Duplicate elements are allowed. If the Array contains more than one element equal to
Note For every element tested, This method is an O(log See also
Applies toBinarySearch(Array, Int32, Int32, Object)Searches a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value.
Parametersarray Array The sorted one-dimensional Array to search. index Int32 The starting index of the range to search. length Int32 The length of the range to search. value Object The object to search for. ReturnsInt32The index of the specified Exceptions
-or-
-or-
RemarksThis method does not support searching arrays that contain negative indexes. If the Array does not
contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~ in C#, Either Note If Duplicate elements are allowed. If the Array contains more than one element equal to
Note For every element tested, This method is an O(log See also
Applies toBinarySearch(Array, Int32, Int32, Object, IComparer)Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer interface.
Parametersarray Array The sorted one-dimensional Array to search. index Int32 The starting index of the range to search. length Int32 The length of the range to search. value Object The object to search for. ReturnsInt32The index of the specified Exceptions
-or-
-or-
RemarksThis method does not support searching arrays that contain negative indexes. If the Array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~ in C#, The comparer customizes how the elements are compared. For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches. If If Note If Duplicate elements are allowed. If the Array contains more than one element equal to
Note For every element tested, This method is an O(log See also
Applies toBinarySearch<T>(T[], T)Searches an entire one-dimensional sorted array for a specific element, using the IComparable<T> generic interface implemented by each element of the Array and by the specified object.
Type ParametersT The type of the elements of the array. Parametersarray T[] The sorted one-dimensional, zero-based Array to search. value T The object to search for. ReturnsInt32The index of the specified ExceptionsExamplesThe following code example demonstrates the Sort<T>(T[]) generic method overload and the BinarySearch<T>(T[], T) generic method overload. An array of strings is created, in no particular order. The array is displayed, sorted, and displayed again. Arrays must be sorted in order to use the BinarySearch method. Note The calls to the Sort and BinarySearch generic methods do not look any different from calls to their nongeneric counterparts, because Visual Basic, F#, C#, and C++ infer the type of the generic type parameter from the type of the first argument. If you use the Ildasm.exe (IL Disassembler) to examine the Microsoft intermediate language (MSIL), you can see that the generic methods are being called. The BinarySearch<T>(T[],
T) generic method overload is then used to search for two strings, one that is not in the array and one that is. The array and the return value of the BinarySearch method are passed to the
RemarksThis method does not support searching arrays that contain negative indexes. If
Duplicate elements are allowed. If the Array contains more than one element equal to
Note For every element tested, This method is an O(log See also
Applies toBinarySearch<T>(T[], T, IComparer<T>)Searches an entire one-dimensional sorted array for a value using the specified IComparer<T> generic interface.
Type ParametersT The type of the elements of the array. Parametersarray T[] The sorted one-dimensional, zero-based Array to search. value T The object to search for. ReturnsInt32The
index of the specified Exceptions
ExamplesThe following example demonstrates the Sort<T>(T[], IComparer<T>) generic method overload and the BinarySearch<T>(T[], T, IComparer<T>) generic method overload. The code example defines an alternative comparer for strings, named The array is displayed, sorted, and displayed again. Arrays must be sorted in order to use the BinarySearch method. The BinarySearch<T>(T[], T, IComparer<T>) generic method overload is then used to search for
two strings, one that is not in the array and one that is. The array and the return value of the BinarySearch<T>(T[], T, IComparer<T>) method are passed to the
RemarksThis method does not support searching arrays that
contain negative indexes. If the Array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~ in C#, The comparer customizes how the elements are compared. For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches. If If Note If Duplicate elements
are allowed. If the Array contains more than one element equal to
Note For every element tested, This method is
an O(log See also
Applies toBinarySearch<T>(T[], Int32, Int32, T)Searches a range of elements in a one-dimensional sorted array for a value, using the IComparable<T> generic interface implemented by each element of the Array and by the specified value.
Type ParametersT The type of the elements of the array. Parametersarray T[] The sorted one-dimensional, zero-based Array to search. index Int32 The starting index of the range to search. length Int32 The length of the range to search. value T The object to search for. ReturnsInt32The index of the specified Exceptions
-or-
-or-
RemarksThis method does not support searching arrays that contain negative indexes. If the array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~ in C#,
Duplicate elements are allowed. If the Array contains more
than one element equal to
Note For every element tested, This method is an O(log See also
Applies toBinarySearch<T>(T[], Int32, Int32, T, IComparer<T>)Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer<T> generic interface.
Type ParametersT The type of the elements of the array. Parametersarray T[] The sorted one-dimensional, zero-based Array to search. index Int32 The starting index of the range to search. length Int32 The length of the range to search. value T The object to search for. ReturnsInt32The index of the specified Exceptions
-or-
-or-
RemarksThis method does not
support searching arrays that contain negative indexes. If the array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~ in C#, The comparer customizes how the elements are compared. For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches. If If Duplicate elements are allowed. If the Array contains more than one element equal to
Note For every element tested, This method is an O(log See also
Applies toWhen you place objects in order beginning with the object that has the lowest value you are sorting in?For example, ordering a set of numbers from greatest to smallest value means that you're arranging them in descending order. Say you have the following numbers: 49, 80, 56, 11, 20 . Sorting them in descending order would look like this: 80, 56, 49, 20, 11 .
Is a mechanism that enables one class to acquire all the behaviors and attributes of another class?Inheritance is a mechanism that allows one class to gain the properties of another class, in the same way, that a child inherits some attributes from each of their parents. Inheritance allows programmers to create a new class that reuses the data members and methods of an existing class.
Which field holds the number of rows in a two dimensional array?When working with two-dimensional arrays, the length field holds the number of rows in an array and each row has a length field that holds the number of columns in the row.
When you instantiate an object that is a member of a subclass the?When you instantiate an object that is a member of a subclass two constructors are called: the base class and the derived class. The superclass constructors must execute first and then the subclass constructor executes.
|