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;

}

 

相关文章:

  • 2021-11-06
  • 2022-12-23
  • 2021-07-07
  • 2021-08-27
  • 2021-05-30
  • 2021-06-24
  • 2021-11-27
猜你喜欢
  • 2022-12-23
  • 2021-09-23
  • 2021-09-14
相关资源
相似解决方案