How to filter a list in-place with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

        // Filter in-place: keep only even numbers
        numbers.RemoveAll(n => n % 2 != 0);

        // Output the filtered list
        Console.WriteLine(string.Join(", ", numbers)); 
    }
}



/*
run:

2, 4, 6, 8, 10, 12

*/

 



answered Jul 13, 2025 by avibootz

Related questions

...