CSharp - Specify the default value for an empty sequence in DefaultIfEmpty operator

Description

Specify the default value for an empty sequence in DefaultIfEmpty operator

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*  w  ww . j a va  2s . c o m*/
{
    static void Main(string[] args)
    {
        string[] codeNames = {"Python", "Java", "A", "Bash", "C++", "Oracle"};
        
        string name = codeNames.Where(n => n.Equals("Jones")).DefaultIfEmpty("Missing").First();
        Console.WriteLine(name);

    }
}

Result

Related Topic