?
Solved

C#: Check what type a class is.

Posted on 2009-07-05
1
Medium Priority
?
1,315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

800 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