Solved

C#: Check what type a class is.

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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