typeof, alternately also typeOf, and TypeOf, is an
operator provided by several
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s to determine the
data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
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
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an a ...
. In some languages, such as
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to:
* Visual Basic (.NET), the current version of Visual Basic launched in 2002 which runs on .NET
* Visual Basic (classic), the original Visual Basic suppo ...
, 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 computer programming, run-time type information or run-time type identification (RTTI) is a feature of some programming languages (such as C++, Object Pascal, and Ada) that exposes information about an object's data type at runtime. Run-time ...
.
In other languages, such as
C# or
D and, to some degree, in C (as part of nonstandard extensions and
proposed standard revisions),
[Typeof]
in "Using the GNU Compiler Collection". the typeof operator returns the
static type
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
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)
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.
In a non-standard (GNU) extension of the
C programming language
C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
, typeof may be used to define a general macro for determining the maximum value of two parameters:
#define max(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
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
:
function isNumber(n)
TypeScript
In
TypeScript
TypeScript (abbreviated as TS) is a high-level programming language that adds static typing with optional type annotations to JavaScript. It is designed for developing large applications and transpiles to JavaScript. It is developed by Micr ...
:
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 ntegerIs Integer? " & TypeOf refInteger Is Integer)
MsgBox("TypeOf Object ntegerIs Double? " & TypeOf refInteger Is Double)
Dim refForm As Object = New System.Windows.Forms.Form
MsgBox("TypeOf Object ormIs Form? " & TypeOf refForm Is System.Windows.Forms.Form)
MsgBox("TypeOf Object ormIs Label? " & TypeOf refForm Is System.Windows.Forms.Label)
MsgBox("TypeOf Object ormIs Control? " & TypeOf refForm Is System.Windows.Forms.Control)
MsgBox("TypeOf Object ormIs IComponent? " & TypeOf refForm Is System.ComponentModel.IComponent)
See also
*
sizeof
sizeof is a unary operator in the C and C++ programming languages that evaluates to the storage size of an expression or a data type, measured in units sized as char. Consequently, the expression sizeof(char) evaluates to 1. The number of b ...
*
decltype
*
Type introspection
In computing, type introspection is the ability of a program to ''examine'' the type or properties of an object
at runtime.
Some programming languages possess this capability.
Introspection should not be confused with reflection, which goes a ...
References
{{Reflist
Operators (programming)