In this lesson, we’re going to talk about working with Collections, specifically the kind that holds generic types. Collections perform the same type of function in code that arrays do, but tend to be easier to work with. This is because Collections are strongly typed – meaning they enforce a very specific class type to be entered – and easily allow for elements to be added or removed. This lesson will primarily look at the List Collection, which is generic (as indicated by the angled brackets):

cs-asp-046---working-with-the-list-collection.001

A List is a type, within the .NET Framework, that can store multiple items (just like an array) of a single type determined within angled brackets. The <T> part refers to the class definition for List, that can take in any type and then wherever T is referenced throughout the class, it assumes the type given.

cs-asp-046---working-with-the-list-collection.002

The <T> within the angled brackets means that the list is currently generic, capable of taking in any type. What makes Lists stand out, though, is the ability to determine what the type is by providing it within the angled brackets, such as in these examples:

cs-asp-046---working-with-the-list-collection.003

Tip:

You can make any class or method generic by postfixing <T> after the identifier. The topic of Generics is somewhat advanced, but you will see one of its greatest purposes by using Lists and Dictionaries. Keep in mind that “generic” refers to the class/method definition side of the equation. In actuality, the type handled by a generic class/method becomes specific as soon as you make use of it (through an instance, or calling a method, for example).

Step 1: Create a New Project

Create a new ASP.NET project for this lesson and call it “CS-ASP_046” and include a single resultLabel Control:

cs-asp-046---working-with-the-list-collection.006

Also, add a Car class, and within it write the following:

cs-asp-046---working-with-the-list-collection.007

In the Default.aspx.cs, let’s create several instances of a Car object:

cs-asp-046---working-with-the-list-collection.008

Step 2: Make A List<Car> and Add Cars To It

And now let’s make a List of type Car and add each car to the List using the Add() helper method available to the List instance. Note that once the generic type becomes specific (in this case, of type Car), you can only add cars to the List:

cs-asp-046---working-with-the-list-collection.009

Since a List is much like an array, you can do many of the same things you’ve come to expect from arrays:

cs-asp-046---working-with-the-list-collection.010

cs-asp-046---working-with-the-list-collection.011

Step 3: Previewing the Power of Lambda Expressions

One of the most useful features of Collections in general is their built-in methods that utilize Lambda Expressions. While outside of the scope of this lesson, a Lambda Expression could be thought of as a compact method that you can define and insert into the input argument of another method. To give you a sense of the simplicity and power of Lambda Expressions we’ll briefly look here at some typical usage scenarios. While the mechanics of how this all works is founded upon more advanced topics, such as Generic Delegates and Anonymous Methods, what should be clear is how Collections allow for all kinds of data manipulation and retrieval at the hands of very little code.

Here a Lambda Expression is used in the FindAll() method to retrieve a List<Car> of all elements in cars that match “White” for the Color field (if you wanted to find cars of a different color, simply change the match string to a different color):

cs-asp-046---working-with-the-list-collection.012

Another common scenario is to change values for a particular field of each element in the List. You can do that by accessing the ForEach() method and passing in the following Lambda Expression – in this case applying a new coat of paint to each car in the List:

cs-asp-046---working-with-the-list-collection.013

If you want to filter the List by cars made within a certain year, you can do so with the Exists() method. And since it returns a bool – depending on if a match is found with the Lambda Expression input argument – we can wrap it in a conditional as follows:

cs-asp-046---working-with-the-list-collection.014


Lessons in this Course