Any

Input: IEnumerable<TSource>
Optional lambda expression:TSource => TResult

Any returns true if the given expression is true for at least one element.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        bool hasAThree = new int[] { 2, 3, 4 }.Any(n => n == 3);  // true;
        Console.WriteLine(hasAThree);
    }
}

The output:


True

Any can do everything that Contains can do, and more:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        bool hasABigNumber = new int[] { 2, 3, 4 }.Any(n => n > 10);  // false;
        Console.WriteLine(hasABigNumber);
    }
}

The output:


False

Calling Any without an expression returns true if the sequence has one or more elements.

Here's another way to write the preceding query:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        bool hasABigNumber = new int[] { 2, 3, 4 }.Where(n => n > 10).Any(); 
        
        Console.WriteLine(hasABigNumber);
    }
}

The output:


False
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.