ラムダ式、最初に見たときは変態的だと思ったけど、もうずいぶん染まった
要素の削除 - 匣の向こう側 - あまりに.NETな
↑これに対して、C# だと標準で機能そろってますよね。
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { var numbers = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; numbers.RemoveAll(x => x % 2 == 0); foreach (int n in numbers) Console.WriteLine(n); } } }
さらに LINQ 版。
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; foreach (int n in numbers.Where(x => x % 2 != 0)) Console.WriteLine(n); } }