Time for action – checking Any()

Follow the given steps:

  1. Copy the following as a query to LINQPad:
    string[] names = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
    bool z  = names.Any();
    bool x = names.Any(c => c.Length == 5);
    z.Dump("There are some elements");
    x.Dump("There is at least one element with length 5");
  2. Run the query and you will get the following output:
    Time for action – checking Any()

What just happened?

Both x and z will be true, in this case, as the source collection (names in this case) has some elements and a couple of them have a length of 5.

Unlike Single(), Any() doesn't complain if there is no element in the source collection or any that matches the Lambda ...

Get .NET 4.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.