How to get all items of a type from a list of inherited type.

AllEnemies Is a list of type CAR and Contains Two types, Enemy and AiCar that Both inherit from the class named CAR. I would like to only do a specific action on the type Enemy.

How do you translate this Foreach Loop into a For loop as i would like to remove elements from the list whilst iterating over it.

    foreach (Enemy TheEnemy in AllEnemies.OfType<Enemy>())

If possible, i would like the code to be clean and Future proof. Meaning no If(TheEnemy.gettype == “Enemy”) ETC

The newer C# syntax has a clean way of typecasting that also gives you a typed variable:

for (int k=0; k<AllEnemies.Count; k++)
    var car = AllEnemies[k];
    if (car is Enemy enemy)
        // Do something with your enemy
        // ...