Filter Even Numbers from an Array
Filtering out even numbers from an array is concise and efficient with LINQ.
int[] nums = { 1, 2, 3, 4, 5, 6 }; var evens = nums.Where(n => n % 2 == 0); Console.WriteLine(string.Join(", ", evens));
Explanation: The Where method filters the array based on the condition n % 2 == 0, leaving only the even numbers, which are then printed.