ImmutableList<T>.BinarySearch 方法
定义
重载
| BinarySearch(T) |
使用默认的比较器在整个已排序的列表中搜索元素,并返回该元素从零开始的索引。Searches the entire sorted list for an element using the default comparer and returns the zero-based index of the element. |
| BinarySearch(T, IComparer<T>) |
使用指定的比较器在整个已排序的列表中搜索元素,并返回该元素从零开始的索引。Searches the entire sorted list for an element using the specified comparer and returns the zero-based index of the element. |
| BinarySearch(Int32, Int32, T, IComparer<T>) |
使用指定的比较器在已排序列表的某个元素范围中搜索元素,并返回该元素从零开始的索引。Searches a range of elements in the sorted list for an element using the specified comparer and returns the zero-based index of the element. |
BinarySearch(T)
使用默认的比较器在整个已排序的列表中搜索元素,并返回该元素从零开始的索引。Searches the entire sorted list for an element using the default comparer and returns the zero-based index of the element.
public:
virtual int BinarySearch(T item);
public:
int BinarySearch(T item);
public int BinarySearch (T item);
abstract member BinarySearch : 'T -> int
override this.BinarySearch : 'T -> int
member this.BinarySearch : 'T -> int
Public Function BinarySearch (item As T) As Integer
参数
- item
- T
要定位的对象。The object to locate. 对于引用类型,该值可以为 null。The value can be null for reference types.
返回
如果找到项,则为已排序的列表中项的从零开始的索引;否则为一个负数,该负数是大于项的第一个元素的索引的按位求补。如果没有更大的元素,则为 Count 的按位求补。The zero-based index of item in the sorted List, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.
例外
默认比较器找不到 T 类型的比较器实现。The default comparer cannot find a comparer implementation of the for type T.
适用于
BinarySearch(T, IComparer<T>)
使用指定的比较器在整个已排序的列表中搜索元素,并返回该元素从零开始的索引。Searches the entire sorted list for an element using the specified comparer and returns the zero-based index of the element.
public:
virtual int BinarySearch(T item, System::Collections::Generic::IComparer<T> ^ comparer);
public:
int BinarySearch(T item, System::Collections::Generic::IComparer<T> ^ comparer);
public int BinarySearch (T item, System.Collections.Generic.IComparer<T> comparer);
public int BinarySearch (T item, System.Collections.Generic.IComparer<T>? comparer);
abstract member BinarySearch : 'T * System.Collections.Generic.IComparer<'T> -> int
override this.BinarySearch : 'T * System.Collections.Generic.IComparer<'T> -> int
member this.BinarySearch : 'T * System.Collections.Generic.IComparer<'T> -> int
Public Function BinarySearch (item As T, comparer As IComparer(Of T)) As Integer
参数
- item
- T
要定位的对象。The object to locate. 对于引用类型,该值可以为 null。The value can be null for reference types.
- comparer
- IComparer<T>
比较元素时要使用的比较器实现,或者为 null,表示使用默认比较器。The comparer implementation to use when comparing elements or null to use the default comparer.
返回
如果找到项,则为已排序的列表中项的从零开始的索引;否则为一个负数,该负数是大于项的第一个元素的索引的按位求补。如果没有更大的元素,则为 Count 的按位求补。The zero-based index of item in the sorted List, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.
例外
比较器为 null,默认比较器找不到 T 类型的比较器实现。comparer is null, and the default comparer cannot find an comparer implementation for type T.
适用于
BinarySearch(Int32, Int32, T, IComparer<T>)
使用指定的比较器在已排序列表的某个元素范围中搜索元素,并返回该元素从零开始的索引。Searches a range of elements in the sorted list for an element using the specified comparer and returns the zero-based index of the element.
public:
virtual int BinarySearch(int index, int count, T item, System::Collections::Generic::IComparer<T> ^ comparer);
public:
int BinarySearch(int index, int count, T item, System::Collections::Generic::IComparer<T> ^ comparer);
public int BinarySearch (int index, int count, T item, System.Collections.Generic.IComparer<T> comparer);
public int BinarySearch (int index, int count, T item, System.Collections.Generic.IComparer<T>? comparer);
abstract member BinarySearch : int * int * 'T * System.Collections.Generic.IComparer<'T> -> int
override this.BinarySearch : int * int * 'T * System.Collections.Generic.IComparer<'T> -> int
member this.BinarySearch : int * int * 'T * System.Collections.Generic.IComparer<'T> -> int
Public Function BinarySearch (index As Integer, count As Integer, item As T, comparer As IComparer(Of T)) As Integer
参数
- index
- Int32
要搜索范围的从零开始的起始索引。The zero-based starting index of the range to search.
- count
- Int32
要搜索的范围的长度。The length of the range to search.
- item
- T
要定位的对象。The object to locate. 对于引用类型,该值可以为 null。The value can be null for reference types.
- comparer
- IComparer<T>
比较元素时要使用的比较器实现,或者为 null,表示使用默认比较器。The comparer implementation to use when comparing elements, or null to use the default comparer.
返回
如果找到项,则为已排序的列表中项的从零开始的索引;否则为一个负数,该负数是大于项的第一个元素的索引的按位求补。如果没有更大的元素,则为 count 的按位求补。The zero-based index of item in the sorted list, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of count.
例外
索引小于 0,或 count小于 0。index is less than 0 or count is less than 0.
索引和 count 不表示列表中的有效范围。index and count do not denote a valid range in the list.
comparer 为 null,默认比较器找不到 T 类型的比较器实现。comparer is null, and the default comparer cannot find an comparer implementation for type T.