SortedList<TKey,TValue>.IndexOfValue(TValue) 方法


在整个 SortedList<TKey,TValue> 中搜索指定的值,并返回第一个匹配项的从零开始的索引。Searches for the specified value and returns the zero-based index of the first occurrence within the entire SortedList<TKey,TValue>.

 int IndexOfValue(TValue value);
public int IndexOfValue (TValue value);
member this.IndexOfValue : 'Value -> int
Public Function IndexOfValue (value As TValue) As Integer



要在 SortedList<TKey,TValue> 中定位的值。The value to locate in the SortedList<TKey,TValue>. 对于引用类型,该值可以为 nullThe value can be null for reference types.


如果找到,则为整个 valueSortedList<TKey,TValue> 第一个匹配项的从零开始的索引;否则为 -1。The zero-based index of the first occurrence of value within the entire SortedList<TKey,TValue>, if found; otherwise, -1.


此方法使用 TValue值类型的默认比较器 Comparer<T>.Default 来确定相等性。This method determines equality using the default comparer Comparer<T>.Default for the value type TValue. Comparer<T>.Default 检查值类型是否 TValue 实现 System.IComparable<T> 并使用该实现(如果可用)。Comparer<T>.Default checks whether the value type TValue implements System.IComparable<T> and uses that implementation, if available. 否则,Comparer<T>.Default 会检查值类型是否 TValue 实现 System.IComparableIf not, Comparer<T>.Default checks whether the value type TValue implements System.IComparable. 如果值类型 TValue 未实现任何一个接口,则此方法使用 Object.EqualsIf the value type TValue does not implement either interface, this method uses Object.Equals.

此方法执行线性搜索;因此,平均执行时间与 Count成正比。This method performs a linear search; therefore, the average execution time is proportional to Count. 也就是说,此方法是一个 O (n)操作,其中 nCount的。That is, this method is an O(n) operation, where n is Count.