Use LINQ to print even numbers from {1,2,3,4,5,6}.
Ready — edit the code above and click Run.
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] nums = { 1, 2, 3, 4, 5, 6 };
var evens = nums.Where(n => n % 2 == 0);
Console.WriteLine(string.Join(", ", evens));
}
}
Try solving on your own first, then reveal the official answer.
Where filters sequences—LINQ is heavily tested in C# interviews.