Chapter 15: Summary

This chapter described the internals of how the foreach loop works and explained which interfaces are required for its execution. In addition, developers frequently filter a collection so that there are fewer items and project the collection so that the items take a different form. Toward that end, this chapter discussed the details of how to use the standard query operators—LINQ introduced collection extension methods on the System.Linq.Enumerable class—to perform collection manipulation.

In the introduction to standard query operators, we detailed the process of deferred execution and emphasized how developers should take care to avoid unintentionally re-executing an expression via a subtle call that enumerates over the collection contents. The deferred execution and resultant implicit execution of standard query operators is a significant factor in code efficiency, especially when the query execution is expensive. Programmers should treat the query object as the query object, not the results, and should expect the query to execute fully even if it executed already. The query object doesn’t know that the results will be the same as they were during a previous execution.

Listing 15.23 appeared in an Advanced Topic section because of the complexity of calling multiple standard query operators one after the other. Although requirements for similar execution may be commonplace, it is not necessary to rely on standard query operators directly. Query expressions,13 a SQL-like syntax for manipulating collections in a way that is frequently easier to code and read, are shown in the next chapter.

The chapter ended with a detailed look at anonymous types and explained why tuples are, in fact, a preferable approach if you have C# 7.0 or later.

________________________________________

13. Starting in C# 3.0.
{{ snackbarMessage }}
;