Get First Or Default with a Not Found Element in CSharp

Description

The following code shows how to get First Or Default with a Not Found Element.

Example


using System;//from w  w  w  .  java  2s . 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.Take(0).FirstOrDefault();
        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