C#: Check what type a class is.

Lets say I have the following classes::
public abstract class Animal
public abstract class MeatEater : Animal
public abstract class PlantEater : Animal

public class Dog : MeatEater

Lets say I create an instance of Dog. I now want to determine if Dog is a meateater or Planteater, how would this be done?

Thank you.
Who is Participating?
Fernando SotoConnect With a Mentor RetiredCommented:
Hi o3h;

By using the "is" operator, see code snippet.

Dog dog = new Dog();
if (dog is MeatEater)
    Console.WriteLine("Dog is a MeatEater");
    Console.WriteLine("Dog is NOT a MeatEater");
if (dog is PlantEater)
    Console.WriteLine("Dog is a PlantEater");
    Console.WriteLine("Dog is NOT a PlantEater");

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.