Time for action – making a list out of IEnumerable<T>

Follow the given steps:

  1. Copy the following code snippet as a query to LINQPad:
    string[] firstNames = { "Samuel", "Jenny", "Joyace", "Sam" };
    List<string> matchingFirstNames = firstNames.Where(name=>name.StartsWith("Sa")).ToList();
    matchingFirstNames.Dump("Name List");
  2. Run the query. You should get the following output:
    Time for action – making a list out of IEnumerable<T>

What just happened?

This proves that ToList<T>() essentially converts the results to a List<T>. This operator can come in particularly handy when we need to use the ForEach() method of List<T> for running some action on each element of the collection. However I don't recommend heavy ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.