LINQ and Immutability

Learn what happens to input collections when working with LINQ.

We'll cover the following

We’ve learned how to use the Where method. It returns a new collection with the elements that meet a condition. But what happens to the input collection when we filter it using the Where method? Let’s find out!

Does LINQ change input collections?

Let’s go back to the previous exercises and print the input collection before and after filtering it with the Where method. This will show us if LINQ changes input collections.

In the below example, use the PrintMovies method to print the movies collection before and after filtering it each time. More movies have been added, as well as a LINQ query to find movies released in 2001.

Notice in the printed messages if the list of movies has the same elements. Are they in the same order?

Get hands-on with 1200+ tech skills courses.