在列表中查找int的索引

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

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

FindIndex似乎是您正在寻找的:

FindIndex(Predicate)

用法:

 list1.FindIndex(x => x==5); 

例:

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

试试IndexOf 。

 List accountList = new List {"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); //} //);