, alternately also , and , is an operator provided by several programming languages to determine the data type of a variable. This is useful when constructing programs that must accept multiple types of data without explicitly specifying the type.
In languages that support polymorphism and type casting, the typeof operator may have one of two distinct meanings when applied to an object. In some languages, such as Visual Basic, the typeof operator returns the dynamic type of the object. That is, it returns the true, original type of the object, irrespective of any type casting. In these languages, the typeof operator is the method for obtaining run-time type information.
In other languages, such as C# or D and, in C (as of C23), the typeof operator returns the static type of the operand. That is, it evaluates to the declared type at that instant in the program, irrespective of its original form. These languages usually have other constructs for obtaining run-time type information, such as .
As of C23 <code>typeof</code> is a part of the C standard. The operator <code>typeof_unqual</code> was also added which is the same as <code>typeof</code>, except it removes cvr-qualification and atomic qualification (differentiating it from <code>decltype</code> in C++).
In a non-standard (GNU) extension of the C programming language, <code>typeof</code> may be used to define a general macro for determining the maximum value of two parameters:
In C++, while there is no <code>typeof</code> operator, there is a <code>decltype</code> operator, which can be used to represent the type of a variable. Although C23 added <code>typeof</code>, it does not exist in C++. C++ also has a <code>typeid</code> operator to get type information.
In C#:
Java does not have a keyword equivalent to <code>typeof</code>. All objects can use <code>Object</code>'s <code>getClass()</code> method to return their class, which is always an instance of the <code>Class</code> class. All types can be explicitly named by appending "<code>.class</code>", even if they are not considered classes, for example <code>int.class</code> and <code>String[].class</code>. There is also the <code>instanceof</code> operator for type introspection which takes an instance and a class name, and returns true for all subclasses of the given class.
In JavaScript:
In TypeScript:
Python has the built-in function .
In VB.NET, the C# variant of "typeof" should be translated into the VB.NET's GetType method. The TypeOf keyword in VB.NET is used to compare an object reference variable to a data type.
The following example uses TypeOf...Is expressions to test the type compatibility of two object reference variables with various data types.