# BinarySearch

> Finds a value in a sorted array by binary search.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Collections](/engine/6000.7/script-reference/unity/collections.md)
* **Assembly:** UnityEngine.ManagedKernelModule

## BinarySearch\<T>(\`\<>\*, int, T)

Finds a value in a sorted array by binary search.

```csharp
public static int BinarySearch<T>(T* ptr, int length, T value) where T : unmanaged, IComparable<T>
```

### Parameters

**** (\[\<T>\*]\(./)): The array to search.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The number of elements to search. Indexes greater than or equal to `length` won't be searched.**** (T): The value to locate.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If the array is not sorted, the value might not be found, even if it's present in the array.

## BinarySearch\<T, U>(\`\<>\*, int, T, T)

Finds a value in a sorted array by binary search using a custom comparison.

```csharp
public static int BinarySearch<T, U>(T* ptr, int length, T value, U comp) where T : unmanaged where U : IComparer<T>
```

### Parameters

**** (\[\<T>\*]\(./)): The array to search.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The number of elements to search. Indexes greater than or equal to `length` won't be searched.**** (T): The value to locate.**** (T): The comparison function used to determine the relative order of the elements.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If the array is not sorted, the value might not be found, even if it's present in the array.

## BinarySearch\<T>(NativeArray\<T>, T)

Finds a value in this sorted array by binary search.

```csharp
public static int BinarySearch<T>(this NativeArray<T> container, T value) where T : unmanaged, IComparable<T>
```

### Parameters

**** (\[NativeArray\<T>]\(/engine/6000.7/script-reference/unity/collections/nativearray1)): The array to search.**** (T): The value to locate.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If the array is not sorted, the value might not be found, even if it's present in this array.

## BinarySearch\<T, U>(NativeArray\<T>, T, T)

Finds a value in this sorted array by binary search using a custom comparison.

```csharp
public static int BinarySearch<T, U>(this NativeArray<T> container, T value, U comp) where T : unmanaged where U : IComparer<T>
```

### Parameters

**** (\[NativeArray\<T>]\(/engine/6000.7/script-reference/unity/collections/nativearray1)): The array to search.**** (T): The value to locate.**** (T): The comparison function used to determine the relative order of the elements.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If the array is not sorted, the value might not be found, even if it's present in this array.

## BinarySearch\<T>(ReadOnly, T)

Finds a value in a sorted array by binary search.

```csharp
public static int BinarySearch<T>(this NativeArray<T>.ReadOnly container, T value) where T : unmanaged, IComparable<T>
```

### Parameters

**** (\[ReadOnly]\(/engine/6000.7/script-reference/unity/collections/nativearray1/readonly)): **** (T): The value to locate.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If the array is not sorted, the value might not be found, even if it's present in the array.

## BinarySearch\<T, U>(ReadOnly, T, T)

Finds a value in a sorted array by binary search.

```csharp
public static int BinarySearch<T, U>(this NativeArray<T>.ReadOnly container, T value, U comp) where T : unmanaged where U : IComparer<T>
```

### Parameters

**** (\[ReadOnly]\(/engine/6000.7/script-reference/unity/collections/nativearray1/readonly)): **** (T): The value to locate.**** (T):&#x20;

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If the array is not sorted, the value might not be found, even if it's present in the array.

## BinarySearch\<T>(NativeList\<T>, T)

Finds a value in this sorted list by binary search.

```csharp
public static int BinarySearch<T>(this NativeList<T> container, T value) where T : unmanaged, IComparable<T>
```

### Parameters

**** (\[NativeList\<T>]\(/engine/6000.7/script-reference/unity/collections/nativelist1)): The list to search.**** (T): The value to locate.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                   |
| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted list. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If this list is not sorted, the value might not be found, even if it's present in this list.

## BinarySearch\<T, U>(NativeList\<T>, T, T)

Finds a value in this sorted list by binary search using a custom comparison.

```csharp
public static int BinarySearch<T, U>(this NativeList<T> container, T value, U comp) where T : unmanaged where U : IComparer<T>
```

### Parameters

**** (\[NativeList\<T>]\(/engine/6000.7/script-reference/unity/collections/nativelist1)): The list to search.**** (T): The value to locate.**** (T): The comparison function used to determine the relative order of the elements.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                   |
| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted list. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If this list is not sorted, the value may not be found, even if it's present in this list.

## BinarySearch\<T>(UnsafeList\<T>, T)

Finds a value in this sorted list by binary search.

```csharp
public static int BinarySearch<T>(this UnsafeList<T> container, T value) where T : unmanaged, IComparable<T>
```

### Parameters

**** (\[UnsafeList\<T>]\(/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafelist1)): The list to search.**** (T): The value to locate.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                   |
| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted list. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If this list is not sorted, the value might not be found, even if it's present in this list.

## BinarySearch\<T, U>(UnsafeList\<T>, T, T)

Finds a value in this sorted list by binary search using a custom comparison.

```csharp
public static int BinarySearch<T, U>(this UnsafeList<T> container, T value, U comp) where T : unmanaged where U : IComparer<T>
```

### Parameters

**** (\[UnsafeList\<T>]\(/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafelist1)): The list to search.**** (T): The value to locate.**** (T): The comparison function used to determine the relative order of the elements.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                   |
| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted list. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If this list is not sorted, the value might not be found, even if it's present in this list.

## BinarySearch\<T>(ReadOnlySpan\<T>, T)

Finds a value in this sorted ReadOnlySpan by binary search.

```csharp
public static int BinarySearch<T>(this ReadOnlySpan<T> roSpan, T value) where T : unmanaged, IComparable<T>
```

### Parameters

**** (\[ReadOnlySpan\<T>]\(https\://learn.microsoft.com/dotnet/api/system.readonlyspan-1)): The ReadOnlySpan to search.**** (T): The value to locate.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                           |
| ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted ReadOnlySpan. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If this ReadOnlySpan is not sorted, the value might not be found, even if it's present in this ReadOnlySpan.

## BinarySearch\<T, U>(ReadOnlySpan\<T>, T, T)

Finds a value in this sorted ReadOnlySpan by binary search using a custom comparison.

```csharp
public static int BinarySearch<T, U>(this ReadOnlySpan<T> roSpan, T value, U comp) where T : unmanaged where U : IComparer<T>
```

### Parameters

**** (\[ReadOnlySpan\<T>]\(https\://learn.microsoft.com/dotnet/api/system.readonlyspan-1)): The ReadOnlySpan to search.**** (T): The value to locate.**** (T): The comparison function used to determine the relative order of the elements.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                           |
| ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted ReadOnlySpan. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If this ReadOnlySpan is not sorted, the value might not be found, even if it's present in this ReadOnlySpan.

## BinarySearch\<T>(NativeSlice\<T>, T)

Finds a value in this sorted slice by binary search.

```csharp
public static int BinarySearch<T>(this NativeSlice<T> container, T value) where T : unmanaged, IComparable<T>
```

### Parameters

**** (\[NativeSlice\<T>]\(/engine/6000.7/script-reference/unity/collections/nativeslice1)): The slice to search.**** (T): The value to locate.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted slice. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If this slice is not sorted, the value might not be found, even if it's present in this slice.

## BinarySearch\<T, U>(NativeSlice\<T>, T, T)

Finds a value in this sorted slice by binary search using a custom comparison.

```csharp
public static int BinarySearch<T, U>(this NativeSlice<T> container, T value, U comp) where T : unmanaged where U : IComparer<T>
```

### Parameters

**** (\[NativeSlice\<T>]\(/engine/6000.7/script-reference/unity/collections/nativeslice1)): The slice to search.**** (T): The value to locate.**** (T): The comparison function used to determine the relative order of the elements.

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                    |
| ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The zero-based index of the value to find in the sorted slice. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length. |

### Remarks

If this slice is not sorted, the value might not be found, even if it's present in this slice.
