Solved

C#: Check what type a class is.

Posted on 2009-07-05
1
1,310 Views
Last Modified: 2012-05-07
Hi,
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.
0
Comment
Question by:o3h
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 24780100
Hi o3h;

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

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

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question