There is a collection:

List<string> str_list = new List<string>(); str_list.Add("string1"); str_list.Add("string1"); str_list.Add("string2"); str_list.Add("string3"); str_list.Add("string2"); 

It is necessary to delete the items under the indices: str_list[1] and str_list[3] .

Of course, I know that the collection has a RemoveAt method, but you can't do this with it:

 str_list.RemoveAt(1); str_list.RemoveAt(3); 

Because after executing str_list.RemoveAt(1) [3] will be [2].

The value can not, so they can be repeated.

Is it possible to somehow immediately set the range for deleting items by index?

    2 answers 2

    If you have a continuous range of indices [a, b] , do this:

     for (int i = 0; i < b - a + 1; i++) coll.RemoveAt(a); 

    If you have an incomplete list of indexes, sort them in descending order:

     foreach (var idx in indices.OrderByDescending(i => i)) call.RemoveAt(idx); 

    If the indices can repeat, add Distinct : indices.Distinct().OrderByDescending(i => i) .

    There is no built-in method, as far as I know.

    • and in the second example, not foreach should be? - Grundy
    • @Grundy: Correct, really. Thank! - VladD
     str_list.RemoveAt("string1"); 

    If I am not mistaken

    • "The value can not be, so they can be repeated." - Pavel Mayorov
    • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky