List<T>.FindIndex 方法 (Predicate<T>)

搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中第一个匹配元素的从零开始的索引。

命名空间:   System.Collections.Generic
程序集:  mscorlib(mscorlib.dll 中)

public int FindIndex(
	Predicate<T> match
)

参数

match
Type: System.Predicate<T>

Predicate<T> 委托,用于定义要搜索的元素的条件。

返回值

Type: System.Int32

如果找到与 match 定义的条件相匹配的第一个元素,则为该元素的从零开始的索引;否则为 -1。

//其中的t实际上是_itemList传过来的参数,这个函数的其实也是再做遍历,只不过判断的谓语,直接用一个匿名函数代替了;

int index  = _itemList.FindIndex(t=>t==sender.GetComponent<BasePartnerItem>());

//也可以写成这样

int index  = _itemList.FindIndex(IsBasePartnerItem);

private bool IsBasePartnerItem(BasePartnerItem bpi)

{

  if(bpi == ==sender.GetComponent<BasePartnerItem>())

    return true;

  else

    return false;

}

原文地址:https://www.cnblogs.com/kuluodisi/p/5714291.html