HOME

TheInfoList



OR:

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. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s to determine the
data type In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
of a
variable Variable may refer to: * Variable (computer science), a symbolic name associated with a value and whose associated value may be changed * Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
. 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 ...
. 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 (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic (cl ...
, the typeof operator returns the
dynamic type In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
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 typ ...
. In other languages, such as C# or D 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 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 to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
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 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 typ ...
.


Examples

In a non-standard (GNU) extension of the
C programming language ''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
, typeof may be used to define a general macro for determining the maximum value of two parameters: #define max(a,b) () 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) In
VB.NET Visual Basic, originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visua ...
, 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
orm Orm (in Old Norse and in modern Danish, Swedish, Norwegian (bokmål and nynorsk) the word for "snake", "worm" or "dragon") became an Anglo-Saxon personal name during period of the Danelaw. Orm may also refer to: * Orm or Ormin, the author of ...
Is Form? " & TypeOf refForm Is System.Windows.Forms.Form) MsgBox("TypeOf Object
orm Orm (in Old Norse and in modern Danish, Swedish, Norwegian (bokmål and nynorsk) the word for "snake", "worm" or "dragon") became an Anglo-Saxon personal name during period of the Danelaw. Orm may also refer to: * Orm or Ormin, the author of ...
Is Label? " & TypeOf refForm Is System.Windows.Forms.Label) MsgBox("TypeOf Object
orm Orm (in Old Norse and in modern Danish, Swedish, Norwegian (bokmål and nynorsk) the word for "snake", "worm" or "dragon") became an Anglo-Saxon personal name during period of the Danelaw. Orm may also refer to: * Orm or Ormin, the author of ...
Is Control? " & TypeOf refForm Is System.Windows.Forms.Control) MsgBox("TypeOf Object
orm Orm (in Old Norse and in modern Danish, Swedish, Norwegian (bokmål and nynorsk) the word for "snake", "worm" or "dragon") became an Anglo-Saxon personal name during period of the Danelaw. Orm may also refer to: * Orm or Ormin, the author of ...
Is IComponent? " & TypeOf refForm Is System.ComponentModel.IComponent)
In
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
: function isNumber(n) In
TypeScript TypeScript is a free and open source programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are ...
: function (param: typeof existingObject) let newObject: typeof existingObject;


See also

*
sizeof sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of ''char''-sized units. Consequently, the construct ''sizeof (char)'' is guaranteed to be ' ...
*
decltype In the C++ programming language, decltype is a keyword used to query the type of an expression. Introduced in C++11, its primary intended use is in generic programming, where it is often difficult, or even impossible, to express types that depen ...
*
Type introspection In computing, type introspection is the ability of a program to ''examine'' the Data type, type or properties of an Object (computer science), object at Run time (program lifecycle phase), runtime. Some programming languages possess this capability ...


References

{{Reflist Operators (programming)