Solved

C#: Check what type a class is.

Posted on 2009-07-05
1
1,309 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now