CSharp - Use Any operator with condition

Description

Use Any operator with condition

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from   w  w w.  j ava  2s  .c  om*/
{
    static void Main(string[] args)
    {      
        string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
        
        bool any = codeNames.Any(s => s.StartsWith("Z"));
        Console.WriteLine(any);

    }
}

Result

We specify that we want the codeNames that start with the string "Z".

Since there are none, an empty sequence will be returned causing the Any operator to return false.

The following code uses Any operator Where at Least One Element Causes the Predicate to Return True

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from w w  w  . j a v a  2  s . c  o m*/
{
    static void Main(string[] args)
    {
        string[] codeNames = {
          "Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
        
        bool any = codeNames.Any(s => s.StartsWith("J"));
        Console.WriteLine(any);
    }
}

Result

Related Topic