SoFunction
Updated on 2025-03-07

C# method to traverse List and delete an element

This article analyzes the method of C# traversing List and deleting an element. Share it for your reference. The details are as follows:

1. We choose to use the for loop:

for(int i=0;i<;i++)
{
   if(list[i])
  {
    (i);
  }
}

If this cycle is definitely wrong.
{A B C D E F G H}  Suppose that the current traversal is D (i=3), remove, and then traversal i=4(F), E(i=3) is skipped

2. We use reverse order traversal, and this problem is solved.

for(int i=-1;i>=0;i--)
{
   if(list[i])
  {
    (i);
  }
}

I hope this article will be helpful to everyone's C# programming.