Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Examples  





2 See also  





3 References  














typeof







Русский

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


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,[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]orD[3] and, to some degree, in C (as part of nonstandard extensions and proposed standard revisions),[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 typeid.

Examples[edit]

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:

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

InC#:

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

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.[6][7]InVB.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)

InJavaScript:

function isNumber(n)
{
  return ( typeof n === 'number' );
}

InTypeScript:[8]

function (param: typeof existingObject) { ... }
let newObject: typeof existingObject;

See also[edit]

References[edit]

  1. ^ "TypeOf Operator (Visual Basic)". MSDN. Archived from the original on Nov 28, 2016.
  • ^ "typeof (C#)". MSDN. Archived from the original on Sep 10, 2016.
  • ^ "Declarations - D Programming Language 1.0". Digital Mars. Dec 30, 2012. Archived from the original on Oct 7, 2023.
  • ^ "Typeof" in "Using the GNU Compiler Collection".
  • ^ Meneide, JeanHeyd (2021-03-07). "Not-So-Magic - typeof(…) in C | r2". Open Standards. Retrieved 2021-12-02.
  • ^ "N2927: Not-so-magic - typeof for C". Open Standards. 2022-02-02. Archived from the original on Dec 1, 2023.
  • ^ "Consider renaming remove_quals" (PDF). Open Standards. 2022-02-06. Archived (PDF) from the original on Feb 17, 2024.
  • ^ "Using 'typeof' to infer a type". Learn TypeScript. Retrieved 2022-01-28.

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Typeof&oldid=1211286391"

    Category: 
    Operators (programming)
     



    This page was last edited on 1 March 2024, at 20:00 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki