Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I have List which Contains the elements 99,86,87,100,101,104
From these I want to pop the elements from list from 104 to 87.
i.e remaining List should be 99,86. So how can I dynamically remove elements from 104 to 87
Posted

Try:
C#
List<int> list = new List<int>() { 99, 86, 87, 100, 101, 104 };
int index = list.IndexOf(87);
while (index >= 0 && index < list.Count)
    {
    list.RemoveAt(index);
    }
 
Share this answer
 
Starting with .NET 2.0, you can use the 'RemoveRange operator:
C#
List<int> intList = new List<int>{99,86,87,100,101,104};

intList.RemoveRange(intList.IndexOf(87),4);
 
Share this answer
 
You have 2 options

1. Create a new list with the elements in that you want
2. iterate over the list in reverse for example

C#
    //create list
    List<int> data = new List<int>();
    for (int i = 0; i < 100; i++)
    {
        data.Add(i);
    }

    //remove elements that are greater than 90
    for(int x = 99; x >= 0; x--)
    {
        if(data[x] > 90)
        {
            int c = data[x];
            data.Remove(c);
        }
    }
</int></int>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900