Solved

C#: Check what type a class is.

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

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 aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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