Use First, Last with expression : First « LINQ « C# / CSharp Tutorial






using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;

public class MainClass{
   public static void Main(){
       int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
       var query = numbers.First();
       Console.WriteLine("The first element in the sequence");
       Console.Write(query);
       query = numbers.Last();
       Console.WriteLine("The last element in the sequence");
       Console.Write(query);
       Console.WriteLine("The first even element in the sequence");
       query = numbers.First(n => n % 2 == 0);
       Console.Write(query);
       Console.WriteLine("The last even element in the sequence");
       query = numbers.Last(n => n % 2 == 0);
       Console.Write(query);
   }
}








22.43.First
22.43.1.Use First, Last with expression
22.43.2.First with string method
22.43.3.Retrieving all strings in an array whose length matches that of the shortest string
22.43.4.Use First to return the first matching element as a Product
22.43.5.First operator
22.43.6.Get the first element