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/*ww w .j ava2  s.  com*/
{
    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