Take the 2-minute tour ×

i have List<sting> with 5 entries. [0],[1],[2],[3],[4].

if I use List.Clear() all item are removed.

i need remove till specific item, for example till [1]. that means in my list are just 2 items [0] and [1]. how do that with c#?

share|improve this question
Are you talking about the value "[1]" or the index 1? –  LukeH Jun 16 '11 at 14:04
Man, why is this question voted up? –  R0MANARMY Jun 16 '11 at 14:20

5 Answers 5

up vote 1 down vote accepted

You can use the List.RemoveWhere(Predicate).. Alternatively, you can do a for loop - looping backwards, removing items till the item you're after ie

for(var i = List.Count()-1; i>=0; i--) {
   var item = List[i];
   if (item != "itemThatYourLookingFor") {
share|improve this answer

If want to remove all items after index 1 (that is, retain only the first two items):

if (yourList.Count > 2)
    yourList.RemoveRange(2, yourList.Count - 2);

If you need to remove all items after the item with a value of "[1]", regardless of its index:

int index = yourList.FindIndex(x => x == "[1]");
if (index >= 0)
    yourList.RemoveRange(index + 1, yourList.Count - index - 1);
share|improve this answer
Don't you mean RemoveRange starting at 0 and removing index items? –  R0MANARMY Jun 16 '11 at 14:10
@R0MANARMY: The question is ambiguous. The OP says "I need remove till specific item", but they also say "that means in my list are just 2 items [0] and [1]". You could be right, but I'll leave my answer as-is for now, until the OP clarifies their requirements. –  LukeH Jun 16 '11 at 14:17
Could just be a cultural bias. We are used to a system where things go from left to right. For us those statements are inconsistent. But if you assume right to left and it means "I want to start on the right side of the list and remove items to the left until I get to the desired element." Then the two statements make sense. –  R0MANARMY Jun 16 '11 at 14:25

You can use the GetRange method.


myList = myList.GetRange(0,2);

..would give you what you are asking for above.

share|improve this answer
but i really need to delete all items which are after [1] –  r.r Jun 16 '11 at 13:55
@Ragims The above code will return a list with just two items: [0],[1]. –  Chris Shouts Jun 16 '11 at 13:57
@Ragims The new list only contains the 2 items [0] and [1], I don't really know what it is you are asking for since this seems to address your problem spot on as it is described in your post. –  heisenberg Jun 16 '11 at 13:57
List<string> strings = new List<string>{"a", "b", "c", "d", "e"};
List<string> firstTwoStrings = strings.Take(2).ToList();
// firstTwoStrings  contains {"a", "b"}

The Take(int count) method will leave you with count items.

share|improve this answer

You can remove a range from a list, giving the starting index and the number of items to remove.

var items = new List<string> {"0", "1", "2", "3", "4", "5"};
var index = items.IndexOf("1") + 1;

if (index >= 0)
    items.RemoveRange(index, items.Count - index);
share|improve this answer
Probably worth noting that this will clear the entire list if the item isn't found. The OP doesn't specify what behaviour they want in that situation. (My answer leaves the list untouched if the item isn't found. Either option is equally (in)valid in the absence of any clarification from the OP.) –  LukeH Jun 16 '11 at 15:24

Your Answer


