为了让我们可以使用索引
int index = myList.FindIndex(a => a.StartsWith("string_to_search"));时,我们确信在字符串中存在myList。
但是如何检查它是否存在于 List 中并获取它的索引(如果存在)?目前,我正在使用以下代码。
int ii=0;
foreach (var item in myList){
if (item == "string_to_search")
Console.WriteLine("Found at index: " + ii);
// index is ii
ii++;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?