Most likely, there is a method
Clear
, so write
newSpectrum.PeakCentroids.Clear()
.
What if you need to remove just part of elements, on some condition? A most universal solution would be:
using CentroidsList = System.Collection.Generic.List<...>;
CentroidsList list = new CentroidsList();
foreach (var element in newSpectrum.PeakCentroids)
if (SomeCondition(element))
list.Add(element);
foreach (var element in list)
newSpectrum.PeakCentroids.Remove(element);
It depends what collection is that. In many cases you can write more effective code using
for
with index, not
foreach
. Traverse the collection in reverse (from
Count - 1
to
0
) and delete required elements. In this case deletion will not disrupt the loop.
—SA