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//from  w  ww. j  ava  2 s  . com
{
    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