List Index Search

前端 未结 2 529
忘掉有多难
忘掉有多难 2020-12-21 23:28

I have a List item

List xmlValue = new List();

In this I have Item {\"English\",\"Spanish\",\"French\",\

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-22 00:27

    I'd opt against using the LINQ extension methods in this case and use an "old-fashioned" loop:

    string search = "English";
    
    var foundIndices = new List(xmlValue.Count);
    for (int i = 0; i < xmlValue.Count; i++) {
        if (xmlValue[i] == search) {
            foundIndices.Add(i);
        }
    }
    

    It's simply more readable like this in my opinion; also, the foundIndices list never holds any unwanted values.

提交回复
热议问题