Time for action – finding the first element that satisfies a condition

Follow the given steps:

  1. Copy the following code snippet as a query to LINQPad:
    string[] names = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
    string firstName = names.First();
    string firstNameConditional = names.First(c => c.Length == 5);
    
    firstName.Dump("First Name");
    firstNameConditional.Dump("First Name with length 5");
  2. Run the query. The following output will be generated:
    Time for action – finding the first element that satisfies a condition

What just happened?

There are two overloaded versions of the method. The first one doesn't take an input and returns the first element of the collection. The second version takes a predicate in the ...

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.