To explain my question: Car, Boat, Airplane are subclasses from Toy class. How can I check which subclass is object of Toy class?
Asked
Active
Viewed 293 times
-2
-
1Add some code. Now it is impossible to say something. What did you do until now? – Julo Mar 25 '18 at 13:43
1 Answers
0
If your code goes like this:
public class Car : Toy
{
//...
}
Then the first thing that comes to my mind: is
Example:
Car myCar = new Car();
if (myCar is Toy)
{
//...
}

MaxB
- 438
- 6
- 15
-
That check can never fail, so I'm not sure what you are demonstrating? – Ron Beyer Mar 25 '18 at 13:59
-
It's just an example of using "is". Might've made it a bit more complicated, that's true – MaxB Mar 25 '18 at 14:01
-
I have found similar question here : https://stackoverflow.com/a/2742288/1773972 you can use `Type.IsSubclassOf` too – Pini Cheyni Apr 10 '18 at 13:28