CSharp - LINQ Any

Introduction

The Any operator returns true if any element of an input sequence matches a condition.

Prototypes

There are two prototypes we cover. The First Any Prototype

public static bool Any<T>(
  this IEnumerable<T> source);

This prototype of the Any operator will return true if the source input sequence contains any elements.

The second prototype of the Any operator returns true if at least one element causes the predicate method delegate to return true.

The source input sequence enumeration stops once the predicate returns true.

public static bool Any<T>(
  this IEnumerable<T> source,
  Func<T, bool> predicate);

Exceptions

ArgumentNullException is thrown if any of the arguments are null.

The following code uses empty sequence with Any operator.

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)
    {
          bool any = Enumerable.Empty<string>().Any();
          Console.WriteLine(any);
          string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
    
          any = codeNames.Any();
          Console.WriteLine(any);
    }
}

Result

Related Topics