CSharp - Use LastOrDefault operator with condition

Description

Use LastOrDefault operator with condition

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program// w  ww .ja  va  2 s .c o  m
{
    static void Main(string[] args)
    {
          string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
    
          string name = codeNames.LastOrDefault(p => p.StartsWith("B"));
          Console.WriteLine(name == null ? "NULL" : name);

          name = codeNames.LastOrDefault(p => p.StartsWith("Z"));
          Console.WriteLine(name == null ? "NULL" : name);
    }
}

Result

Related Topic