搜索與指定謂詞所定義的條件相匹配的元素,並返回整個 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;
}