C# Professional - Basics & OOP - Exercises

talent-agile
42.6K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: ValueTypes - Exercises

Using casting operators

With C#, you can define custom casting implementation between two types. This implementation will be used when casting from one type to the other in your code.

Exercise : Implement an implicit and explicit casting

In the following exercise, you have two classes, Car and Vehicle.

The goal is to implement an implicit casting of Car to Vehicle. The casting must respect these guidelines:

  • The Vehicle.Type property value should be "Car"
  • The Vehicle.Name property should use the car properties to display all car information with the following format : Brand / Model (Year) / License Plate
Implement implicit casting

Be careful when implementing custom casting operators. They can be useful is certain situations, but using them is not very intuitive and can be misleading.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content