Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the C# equivalent to Java's isInstance()?

I know of is and as for instanceof, but what about the reflective isInstance() method?

like image 746
diegogs Avatar asked Nov 11 '08 23:11

diegogs


People also ask

What is C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language in simple words?

What Does C Programming Language (C) Mean? C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.

What is C for computer?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.


2 Answers

bool result = (obj is MyClass); // Better than using 'as' 
like image 144
Ana Betts Avatar answered Oct 11 '22 23:10

Ana Betts


The equivalent of Java’s obj.getClass().isInstance(otherObj) in C# is as follows:

bool result = obj.GetType().IsAssignableFrom(otherObj.GetType()); 

Note that while both Java and C# work on the runtime type object (Java java.lang.Class ≣ C# System.Type) of an obj (via .getClass() vs .getType()), Java’s isInstance takes an object as its argument, whereas C#’s IsAssignableFrom expects another System.Type object.

like image 25
Konrad Rudolph Avatar answered Oct 11 '22 22:10

Konrad Rudolph