0

This will seem a lot like another post I made but I am writing a similar program in C#. I have class Card and class Land which is a subclass of Card. In Java, I used instanceof to determine if it was a child of Card. The object being referred to is held in the variable c I have already tried:

if (typeof (Land).isSubClassOf(typeof(Card))){
//random code
}

What i'm trying to do, in java, would be:

if ( c instanceof Land){
}
  • 1
    Have you seen this ? http://stackoverflow.com/questions/282459/what-is-the-c-sharp-equivalent-to-javas-instanceof-and-isinstance – Lapious Jul 05 '16 at 16:43
  • `YourObject.GetType.IsAssignableFrom(YourParentClass.GetType());` – Khalil Khalaf Jul 05 '16 at 16:44
  • I think your question has created some confusion, which is why you're getting completely different answers. Do you have an *object* and you want to know if that object is of type `Card` or one of its subclasses, or do you have a `Type` and you want to know if that Type is a subtype of another type? – Scott Hannen Jul 05 '16 at 17:13
  • @Lapious I had not yet, but on trying it, it didn't work the way i needed it to – comphunter159 Jul 05 '16 at 17:14
  • @ScottHannen I have an object, upon testing, the marked answer was the only to work the way it needed to. – comphunter159 Jul 05 '16 at 17:15

3 Answers3

3
if(foo is Bar) {
    return (Bar)foo;
}
Draken
  • 3,134
  • 13
  • 34
  • 54
Rahul Hendawe
  • 902
  • 1
  • 14
  • 39
2

For direct inheritance :

static void Main(string[] args)
{
    Land c = new Land();
    bool isCard = c.GetType().IsSubclassOf(typeof(Card));
}

Or:

static void Main(string[] args)
{
    Land c = new Land();
    bool isCard = c is Card;
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • I tried everything else that I saw and nothing worked. Thanks so much @user3185569 – comphunter159 Jul 05 '16 at 17:12
  • the main problem with this answer is that: Card has several descendants(Land, Spell, etc), in the used method, I pass a Card object. if I test the card to see if it is a land using this method isCard will always be true when what i wanted was to see if the card was a Land – comphunter159 Jul 12 '16 at 10:06
1

Don't check for the type and then cast. If you do that you're really checking the type twice. Do this:

var l = c as Land;

If c can be cast as Land then l will contain that. If not it will be null.

If you do this:

if(c is Land)
    l = (Land)c;

Then you're actually inspecting c twice. Once to check whether it is of type Land, and then again to do the actual cast.

C# As keyword

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
  • The check is in the midst of an if statement, thus casting like that results in an error saying that I cannot implicitly cast Land to bool – comphunter159 Jul 05 '16 at 17:19
  • using this method worked, mostly because i had forgotten that i had declared a variable to hold the type of Card, thus by checking directly to that variable, i was able to safely cast. the difference was made when other types of Card started coming into the program – comphunter159 Jul 07 '16 at 16:59
  • This doesn't matter as much if we're just doing a boolean check - is it a given type? But 99% of the time if we need to know *if* it's a certain type then we're going to need to cast to that type. That's where `as` comes in, because we avoid checking and then casting. – Scott Hannen Jul 07 '16 at 17:44