Easy csharp

Filter even numbers with LINQ

Problem

Use LINQ to print even numbers from {1,2,3,4,5,6}.

Hints
  • nums.Where(n => n % 2 == 0)

Your practice code

Ready — edit the code above and click Run.

Solution

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.

Explanation

Where filters sequences—LINQ is heavily tested in C# interviews.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details