Skip to content

Tarik Billa

  • Web Development
    • html
    • vue.js
    • laravel
    • css
    • javascript
    • jquery
    • node.js
    • php
    • asp.net
  • Programming
    • python
    • java
    • c
    • c++
    • c#
  • git
  • android

remove-method

.NET – Remove from a List within a ‘foreach’ loop

May 18, 2023 by Tarik

You can iterate through the list backwards: for (int i = myList.Count – 1; i >= 0; i–) { if (whatever) myList.RemoveAt(i); } In response to your comment about wanting to quit when you find an item that you’re NOT removing, then just using a while loop would be the best solution.

Categories .net Tags .net, foreach, remove-method Leave a comment

Tarik Billa

Software Engineer
tarikbilla@gmail.com
+8801884414000
  • Reuse a hash in YAMLApril 17, 2024
  • Dockerfile: how to redirect the output of a RUN command to a variable?April 16, 2024
  • How to cd to a directory with spaces in the directory name?April 16, 2024
  • Maximum MIME type length when storing the type in a databaseApril 16, 2024
  • What is the difference between Unit, Integration, Regression and Acceptance Testing?April 16, 2024
© 2026 Tarik Billa