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):
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.
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:
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).
Create a new ASP.NET project for this lesson and call it “CS-ASP_046” and include a single resultLabel Control:
Also, add a Car class, and within it write the following:
In the Default.aspx.cs, let’s create several instances of a Car object:
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:
Since a List is much like an array, you can do many of the same things you’ve come to expect from arrays:
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):
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:
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:
Lesson 46 - Working with the List Collection