Virtual Methods in .NET: The "is" keyword

I have an abstract class called Vehicle, and two child abstract classes called Car, and the other called Truck.

In the Vehicle class, I have a virtual method called TowTrailer(int weight). In this method in the Vehicle class, am I allowed to say:

if ( this is Car )
     return "Cars can't tow trailers."
else if ( this is Truck )
     return "Trucks can tow trailers."

I'm confused because on the one hand, since I'm creating the TowTrailer in the base class, it doesn't feel right to say if "this is Car". The answer is no. Vehicle is NOT a car, but a car is a vehicle.

However, since the TowTrailer method will be inherited by a child of either a Car or a Truck, this is perfectly fine, but I'm not sure.

Please let me know when it's OK to use the "is" keyword in a hierarchy. Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
In my opinion, you've got a bigger problem. The fact that you are checking for specific types of children in your base class means that you are designing your hiearchy incorrectly. You're introducing dependencies into your base class that will negatively impact maintainability. The whole point of virtual methods is that your base class doesn't care what the concrete type is; it only knows that the method is available.

The reason you use the is keyword is to test if an object can be converted to a certain type. Being able to be converted is based on the inheritance hierarchy. If you're designing your classes properly--which is tough at times--you shouldn't need the is keyword. In this case I think you've got a bad design. However, the usage of is appears correct.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
That method is virtual. You can then override it in each subclass to make the specific work.

In Car:
override string TowTrailer(int weight)
    return "Cars can't tow trailers.";

Open in new window

In Truck:
override string TowTrailer(int weight)
    return "Trucks can tow trailers."

Open in new window

And unless you have work to do in that method for any vehicle, make it abstract in the superclass.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.