Casting is a common operation in .NET. In this tip, look at how to use the LINQ cast method to perform a cast on an entire list.
- [Instructor] You can't work long as a C# developer without encountering the cast operators and understanding how they work. Here's a classic example: On line nine we have declared a class called person, this is a class that we use in our tour operation, and then on line 14 I created a Customer class that derives from person, and on line 18 I created a Guide class that also derives from person. And then, when I'm using those classes in my code, I can do stuff like this: On line 15 I'm creating a variable of type Guide, then I'm instantiating Guide and putting an instance in the variable, then on line 16 I'm declaring a variable of type Person, and putting the instance of the type Guide class in there, and that's okay because Guide derives from Person.
When I'm trying to go the opposite direction I get a compiler error because the compiler says I can't implicitly convert from person to guide, so I need to do an explicit cast. Here's a way of doing that. And now I've made the compiler happy, there could be still issues at runtime if I had an instance of a type and this variable, they can't be cast excessively to Guide, but that's not an issue in this case. Now, of course, the other way I could've handled this as I could've used the as operator, like that.
What I want to look at in this tip is how to use links cast to do the same kind of operation, but this time for a list of items. So here is one example: We're going to look at some legacy code. Our company has a legacy library that we're not going to update and it returns some information that we need in this GetAllTourStops method. The problem comes here, in the return type, it's of type ArrayList, so I'm populating in ArrayList here in the constructor, with information about our tour stops, and then I'm returning that, here.
Now, what I'd like to do is, I'd like to write a link query that says legacy.GetAllTourStops and then do an orderby and then select out the results. What if I uncomment this, I get an error that says it can't find an implementation of the query pattern for this source ArrayList. In other words, link can't work with ArrayList. There are many ways I could solve this, I could write a loop that would loop through the ArrayList, and I could create a destination list of T, that I could loop through and populate the list.
And that's what I'm going to do, except I'll use the list cast method to do that instead. So I'm going to comment this out again, and I'll start here on line 27, I've got a list of TourStop here so I will, first make an attempt to call legacy.GetAllTourStops. Now it's saying it can't implicitly convert from a an ArrayList to a list of TourStop. So this is where we'll use dot cast, like this, then I way I want to cast it our TourStop, and the next step is to put some parenthesis in there, and then I'm going to call ToList.
So I'm combining two items: Cast takes every single item out of the ArrayList, and cast each of the individual items to the type TourStop. And then, I used it the ToList to convert that to a List of T so I can put it in here. One more example, here in this person source, I have a GetAllCustomers that returns a list of Customer, and I have a GetAllGuides that returns a list of Guides.
I need to cast to a list of Person. You see, I'm getting the same conversion information here, so I'll use the same code I did before, copy this, paste it in here, and then, instead of casting to a TourStop, I will cast this to a Person. And just to verify, I'll copy this line and make a call to GetAllGuides, and I get the same thing.
So you see, it's an elegant way of doing that cast over a large number of items with the Cast method and the ToList method.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.