Get default value if the result is empty with DefaultIfEmpty in CSharp
Description
The following code shows how to get default value if the result is empty with DefaultIfEmpty.
Example
using System;//from w w w.jav a 2s . c o m
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {"HTML","CSS"};
string name =presidents.Where(n => n.Equals("H")).DefaultIfEmpty("Missing").First();
Console.WriteLine(name);
Console.WriteLine(name);
}
}
The code above generates the following result.