This chapter introduced LINQ (Language Integrated Query), a powerful feature for querying data. We showed how to filter an array or collection using LINQ’s where
clause, and how to sort the query results using the orderby
clause. We used the select
clause to select specific properties of an object, and the let
clause to introduce a new range variable to make writing queries more convenient. The StartsWith
method of class string
was used to filter string
s starting with a specified character or series of characters. We used several LINQ extension methods to perform operations not provided by the query syntax—the Distinct
method to remove duplicates from the results, the Any
method to determine if the results contain any items, and the First
method to retrieve the first element in the results.
We introduced the List<T>
generic collection, which provides all the functionality of arrays, along with other useful capabilities such as dynamic resizing. We used method Add
to append new items to the end of the List
, method Insert
to insert new items into specified locations in the List
, method Remove
to remove the first occurrence of a specified item, method RemoveAt
to remove an item at a specified index and method Contains
to determine if an item was in the List
. We used property Count
to get the number of items in the List
, and property Capacity
to determine the number of elements the List
can hold without growing. We use more advanced features of LINQ in later chapters.
In Chapter 10 we take a deeper look at class concepts. We’ll discuss the this
reference, additional constructor concepts, how the runtime manages memory with garbage collection, static
class members, read-only class members, object initializers and operator overloading.