typeof
Page Template:Mono/styles.css has no content.typeof, alternately also Page Template:Mono/styles.css has no content.typeOf, and Page Template:Mono/styles.css has no content.TypeOf, 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,[1] 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#[2] or D[3] and, in C (as of C23),[4][5] 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 Page Template:Mono/styles.css has no content.typeid.
Such an operator may be usable as a value, providing a run-time representation of the type, or as a type itself that can be applied to a value.
Examples
C
As of C23 typeof is a part of the C standard. The operator typeof_unqual was also added which is the same as typeof, except it removes cvr-qualification and atomic qualification (differentiating it from decltype in C++).[6][7]
typeof can be useful for binding to variables of unknown type in macros. In this example, max must use temporary variables to avoid the inputs being evaluated multiple times, and uses typeof to specify the type of these temporary variables. Note that this example uses a non-standard extension to the C language, namely the use of compound statements as expressions (supported e.g. by GNU GCC[8])
#define max(a, b) ({ \
typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; \
})
C++
Script error: No such module "Labelled list hatnote".
C++ provides the decltype and typeid operators.[9][10] decltype, like C’s typeof, is used to abbreviate types and to give types to variables that depend on the type of generic variables, whose type is unknown (e.g. in templates or macros). typeid provides a run-time representation of the type for dynamic dispatch.
struct Double {
double value;
}
const Double* d;
// decltype can be used in type declarations
decltype(d->value) y; // equivalent to double y;
// decltype can be used in trailing return types
// This may be necessary for overloaded operators where, for instance,
// type T + type U may result in type V distinct from T and U
template <typename T, typename U>
auto add(T t, U u) -> decltype(t + u) {
return t + u;
}
The ^^ reflection operator, introduced in C++26, can also be used to obtain a representation of a type as a value.[11]
import std;
using std::string;
using std::meta::info;
struct User {
string name;
int age;
};
int main() {
constexpr info stringClass = ^^string;
constexpr User john {
.name = "John Doe",
.age = 20
};
std::println("The class of {} is {}", john.name, std::meta::identifier_of(^^User));
}
C#
In C#, the System.Object.GetType() method returns a System.Type value representing the run-time type of a value.[12] The typeof operator instead provides the runtime representation of a type and does not determine the type of a value.[13]
// Given an object, returns if it is an integer.
// The "is" operator can be also used to determine this.
public static bool IsInteger(object o)
{
return o.GetType() == typeof(int);
}
Java
java.lang.Object's getClass() method can be used to return the class of any object, which is always an instance of the java.lang.Class class.[14] All types can be converted into a run-time representation value by appending ".class". Despite the name, this is possible even if they are not classes, such as primitives (int.class) or arrays (String[].class).
String s = "Hello, world!";
Class<?> sClass = s.getClass();
Class<String> stringClass = String.class;
Class<Integer> intClass = int.class;
Object obj = /* something here */;
System.out.printf("The class of %s is %s%n", obj, obj.getClass().getName());
JavaScript
The typeof operator in JavaScript returns a string representing the basic run-time type of a value.[15]
function isNumber(n) {
return typeof n === 'number';
}
TypeScript
TypeScript extends the JavaScript typeof operator, allowing its use in the type of another value, representing the static type of the value.
function someFunction(param: typeof existingObject): void {
// ...
}
let newObject: typeof existingObject;
Python
Python has the built-in function type.[16]
print(type(123))
# prints: <class 'int'>
VB.NET
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.
Dim refInteger As Object = 2
MsgBox("TypeOf Object[Integer] Is Integer? " & TypeOf refInteger Is Integer)
MsgBox("TypeOf Object[Integer] Is Double? " & TypeOf refInteger Is Double)
Dim refForm As Object = New System.Windows.Forms.Form
MsgBox("TypeOf Object[Form] Is Form? " & TypeOf refForm Is System.Windows.Forms.Form)
MsgBox("TypeOf Object[Form] Is Label? " & TypeOf refForm Is System.Windows.Forms.Label)
MsgBox("TypeOf Object[Form] Is Control? " & TypeOf refForm Is System.Windows.Forms.Control)
MsgBox("TypeOf Object[Form] Is IComponent? " & TypeOf refForm Is System.ComponentModel.IComponent)
See also
References
Page Template:Reflist/styles.css has no content.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ "Typeof" in "Using the GNU Compiler Collection".
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.
- ^ Lua error in package.lua at line 80: module 'Module:Citation/CS1/Utilities' not found.