0 like 0 dislike
3 views
asked in C sharp (C#) by (3.7k points)

How I can to find if an object is from a class but not superclass?

is object of class

Your answer

pre, code

Preview

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.

1 Answer

0 like 0 dislike
answered by (3.7k points)

Like this:

typeof(SpecifiedClass) == obj.GetType()

Condition:

if (Obj.GetType() == typeof(ClassName)) {}

Example:

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

Related questions

0 like 0 dislike
1 answer 32 views
0 like 0 dislike
1 answer 3 views
0 like 0 dislike
1 answer 6 views
0 like 0 dislike
1 answer 4 views
0 like 0 dislike
1 answer 4 views
0 like 0 dislike
1 answer 24 views
0 like 0 dislike
1 answer 21 views
0 like 0 dislike
1 answer 5 views
0 like 0 dislike
1 answer 18 views
...