Finding the first even number in the numbers list - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

Finding the first even number in the numbers list

Demo Code



using System;/*from   ww  w.  j ava 2 s .  c  om*/
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        int[] numArray = numbers.ToArray();

        int firstEven = Array.Find(numArray, n => n % 2 == 0);
        Console.WriteLine("\t{0}", firstEven);
        // Do the same on the List<int>:
        firstEven = numbers.Find(n => (n % 2) == 0);
        Console.WriteLine("\t{0}", firstEven.ToString());
    }
}

Result


Related Tutorials