在列表中查找int的索引

soa*_*dos 30 c# list find

有没有办法从列表中获取int的索引?寻找像list1.FindIndex(5)我想要在列表中找到5的位置的东西.

abe*_*nky 47

FindIndex似乎是您正在寻找的:

FindIndex(Predicate<T>)

用法:

list1.FindIndex(x => x==5);
Run Code Online (Sandbox Code Playgroud)

例:

// given list1 {3, 4, 6, 5, 7, 8}
list1.FindIndex(x => x==5);  // should return 3, as list1[3] == 5;
Run Code Online (Sandbox Code Playgroud)


jon*_*sca 45

使用.IndexOf()列表的方法.该方法的规范可以在MSDN上找到.


小智 5

List<string> accountList = new List<string> {"123872", "987653" , "7625019", "028401"};

int i = accountList.FindIndex(x => x.StartsWith("762"));
//This will give you index of 7625019 in list that is 2. value of i will become 2.
//delegate(string ac)
//{
//    return ac.StartsWith(a.AccountNumber);
//}
//);
Run Code Online (Sandbox Code Playgroud)