Get last or default for empty array in CSharp

Description

The following code shows how to get last or default for empty array.

Example


using System;//w  w w.j av  a 2  s.  c  o  m
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass {
    public static void Main() {
        string[] presidents = {};

        string name = presidents.LastOrDefault();
        Console.WriteLine(name == null ? "NULL" : name);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ