From the course: Visual Studio Developer Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Convert lists

Convert lists

- [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…

Contents