// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; namespace E { public class Program { public static void Main(string[] args) { List lst = new List { 1, 2, 3, 4 }; Console.WriteLine(lst.Count); for(int i = 0; i < lst.Count; i++) { if(lst[i] == 1) { lst.Remove(lst[i]); } } Console.WriteLine(lst.Count); try { foreach(int v in lst) { if(v == 2) { lst.Remove(v); } } } catch (InvalidOperationException ex) { Console.WriteLine("Ooops"); } } } }