Answers for "c# remove from list while iterating"

C#
4

c# remove from list in foreach

myList.RemoveAll(x => x.SomeProp == "SomeValue");
Posted by: Guest on February-13-2020
1

Remove elements from a list while iterating over it in C#

using System;
using System.Collections.Generic;
using System.Linq;
 
public class Example
{
    public static void Main()
    {
        List<int> list = new List<int>(Enumerable.Range(1, 10));
 
        foreach (int item in list.Reverse<int>())
        {
                list.Remove(item);
            
        }
 
    }
}
Posted by: Guest on April-30-2021
0

remove item from list in for loop c#

var data=new List<string>(){"One","Two","Three"};
for(int i=data.Count - 1; i > -1; i--)
{
    if(data[i]=="One")
    {
        data.RemoveAt(i);
    }
}
Posted by: Guest on October-01-2021

Code answers related to "c# remove from list while iterating"

C# Answers by Framework

Browse Popular Code Answers by Language