List.FindIndex 方法 (Predicate)


搜索与指定谓词所定义的条件相匹配的元素,并返回整个 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;

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM