typeof

{{lowercase}}

typeof, alternately also typeOf, and 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,{{cite web |url=https://msdn.microsoft.com/en-us/library/0ec5kw18(VS.80).aspx |title=TypeOf Operator (Visual Basic) |website= MSDN |url-status=dead |archive-url=https://web.archive.org/web/20161128133108/https://msdn.microsoft.com/en-us/library/0ec5kw18(VS.80).aspx |archive-date= Nov 28, 2016 }} 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#{{cite web |url=https://msdn.microsoft.com/en-us/library/58918ffs(VS.80).aspx |title=typeof (C#) |website=MSDN |url-status=dead |archive-url=https://web.archive.org/web/20160910105030/https://msdn.microsoft.com/en-us/library/58918ffs(VS.80).aspx |archive-date= Sep 10, 2016 }} or D{{Cite web|url=http://digitalmars.com/d/1.0/declaration.html#Typeof|title = Declarations - D Programming Language 1.0 |website=Digital Mars |date=Dec 30, 2012 |url-status=live |archive-url= https://web.archive.org/web/20231007004441/https://digitalmars.com/d/1.0/declaration.html#Typeof|archive-date= Oct 7, 2023 }} and, to some degree, in C (as part of nonstandard extensions and proposed standard revisions),"[https://gcc.gnu.org/onlinedocs/gcc/Typeof.html Typeof]" in "Using the GNU Compiler Collection".{{Cite web|last=Meneide|first=JeanHeyd|date=2021-03-07|title=Not-So-Magic - typeof(…) in C {{!}} r2|url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2685.htm|access-date=2021-12-02|website=Open Standards }} 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 typeid.

Examples

=C#=

In C#:

// 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);

}

=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.{{cite web|url=https://open-std.org/JTC1/SC22/WG14/www/docs/n2927.htm|title=N2927: Not-so-magic - typeof for C |date=2022-02-02 |website=Open Standards |url-status=live |archive-url= https://web.archive.org/web/20231201192011/http://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2927.htm |archive-date= Dec 1, 2023 }}{{cite web|url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2930.pdf|title=Consider renaming remove_quals |date=2022-02-06 |website=Open Standards |url-status=live |archive-url=https://web.archive.org/web/20240217091853/http://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2930.pdf |archive-date= Feb 17, 2024 }}

In a non-standard (GNU) extension of the C programming language, typeof may be used to define a general macro for determining the maximum value of two parameters:

  1. define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })

=Java=

Java does not have a keyword equivalent to typeof. All objects can use Object's getClass() method to return their class, which is always an instance of the Class class. All types can be explicitly named by appending ".class", even if they are not considered classes, for example int.class and String[].class . There is also the instanceof operator for type introspection which takes an instance and a class name, and returns true for all subclasses of the given class.

=JavaScript=

In JavaScript:

function isNumber(n)

{

return ( typeof n === 'number' );

}

=TypeScript=

In TypeScript:{{Cite web|title=Using 'typeof' to infer a type|url=https://learntypescript.dev/08/l4-typeof-infer/|access-date=2022-01-28|website=Learn TypeScript|language=en}}

function (param: typeof existingObject) { ... }

let newObject: typeof existingObject;

=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