Skip to content

M_Cyjb_Collections_ListUtil_BinarySearch__1

CYJB edited this page Mar 20, 2024 · 13 revisions

ListUtil.BinarySearch(T) 方法 (IList(T), Int32, Int32, T, IComparer(T))

在排序 IList(T) 的某个元素范围中搜索值。

Namespace: Cyjb.Collections
Assembly: Cyjb (in Cyjb.dll) Version: 1.0.23+7750dd8e971297c5fa962a3bee37fb78f72793f6

语法

C#

public static int BinarySearch<T>(
	this IList<T> list,
	int index,
	int length,
	T value,
	IComparer<T>? comparer = null
)

参数

 

list
Type: System.Collections.Generic.IList(T)
要搜索的从零开始的排序 IList(T)
index
Type: System.Int32
要搜索的范围的起始索引。
length
Type: System.Int32
要搜索的范围的长度。
value
Type: T
要搜索的对象。
comparer (Optional)
Type: System.Collections.Generic.IComparer(T)
比较元素时要使用的 IComparer(T) 实现。

类型参数

 

T
列表元素的类型。

返回值

Type: Int32
如果找到 value,则为指定 list 中的指定 value 的索引。如果找不到 valuevalue 小于 list 中的一个或多个元素, 则为一个负数,该负数是大于 value 的第一个元素的索引的按位求补。 如果找不到 valuevalue 大于 list 中的任何元素,则为一个负数,该负数是(最后一个元素的索引加 1)的按位求补。

备注

在 Visual Basic 和 C# 中,这个方法可以当成为类型IList(T)的实例方法来调用。在采用实例方法语法调用这个方法时,请省略第一个参数。请参考 扩展方法 (Visual Basic)扩展方法 (C# 编程指南) 获取更多信息。

Exceptions

 

异常 条件
ArgumentNullException listnull
ArgumentOutOfRangeException indexlength 小于 0
ArgumentException indexlength 不指定 list 中的有效范围。
InvalidOperationException comparernull, 且 T 类型没有实现 IComparable(T) 泛型接口。

参见

Reference

ListUtil 类
BinarySearch 重载
Cyjb.Collections 命名空间

Clone this wiki locally