in C# by (9.4k points) recategorized by
0 like 0 dislike
70 views

In programming language C# how can you find if an object is an instance of certain class but not any of that class’s superclasses?

"is" will return "true" even if the object is actually from a superclass.

is object of class

1 Answer

0 like 0 dislike
by (9.4k points) edited by

Use typeof and GetType() method so:

typeof(SpecifiedClass) == obj.GetType()
if (Obj.GetType() == typeof(ClassName))

Examples

class A { }
class B : A { }
A a = new A();
if (a.GetType() == typeof(A)) { } // returns true
A b = new B();
if (b.GetType() == typeof(A)) { } // returns false

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.