码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-28 13:58:17      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

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

}

 

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!