Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

typeof

From Wikipedia, the free encyclopedia

typeof, alternately alsotypeOf, andTypeOf, is anoperator provided by severalprogramming languages to determine thedata type of avariable. This is useful when constructing programs that must accept multiple types of data without explicitly specifying the type.

In languages that supportpolymorphism andtype casting, the typeof operator may have one of two distinct meanings when applied to anobject. In some languages, such asVisual Basic,[1] the typeof operator returns thedynamic 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 obtainingrun-time type information.

In other languages, such asC#[2] orD[3] and, to some degree, in C (as part of nonstandard extensions andproposed standard revisions),[4][5] the typeof operator returns thestatic 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 astypeid.

Examples

[edit]

C#

[edit]

InC#:

// Given an object, returns if it is an integer.// The "is" operator can be also used to determine this.publicstaticboolIsInteger(objecto){returno.GetType()==typeof(int);}

C

[edit]

As ofC23 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]

In a non-standard (GNU) extension of theC 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; })


Java

[edit]

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 fortype introspection which takes an instance and a class name, and returns true for all subclasses of the given class.

JavaScript

[edit]

InJavaScript:

functionisNumber(n){return(typeofn==='number');}

TypeScript

[edit]

InTypeScript:[8]

function(param:typeofexistingObject){...}
letnewObject:typeofexistingObject;

VB.NET

[edit]

InVB.NET, the C# variant of "typeof" should be translated into the VB.NET'sGetType method. TheTypeOf keyword in VB.NET is used to compare an object reference variable to a data type.

The following example usesTypeOf...Is expressions to test the type compatibility of two object reference variables with various data types.

DimrefIntegerAsObject=2MsgBox("TypeOf Object[Integer] Is Integer? "&TypeOfrefIntegerIsInteger)MsgBox("TypeOf Object[Integer] Is Double? "&TypeOfrefIntegerIsDouble)DimrefFormAsObject=NewSystem.Windows.Forms.FormMsgBox("TypeOf Object[Form] Is Form? "&TypeOfrefFormIsSystem.Windows.Forms.Form)MsgBox("TypeOf Object[Form] Is Label? "&TypeOfrefFormIsSystem.Windows.Forms.Label)MsgBox("TypeOf Object[Form] Is Control? "&TypeOfrefFormIsSystem.Windows.Forms.Control)MsgBox("TypeOf Object[Form] Is IComponent? "&TypeOfrefFormIsSystem.ComponentModel.IComponent)

See also

[edit]

References

[edit]
  1. ^"TypeOf Operator (Visual Basic)".MSDN. Archived fromthe original on Nov 28, 2016.
  2. ^"typeof (C#)".MSDN. Archived fromthe original on Sep 10, 2016.
  3. ^"Declarations - D Programming Language 1.0".Digital Mars. Dec 30, 2012.Archived from the original on Oct 7, 2023.
  4. ^"Typeof" in "Using the GNU Compiler Collection".
  5. ^Meneide, JeanHeyd (2021-03-07)."Not-So-Magic - typeof(…) in C | r2".Open Standards. Retrieved2021-12-02.
  6. ^"N2927: Not-so-magic - typeof for C".Open Standards. 2022-02-02.Archived from the original on Dec 1, 2023.
  7. ^"Consider renaming remove_quals"(PDF).Open Standards. 2022-02-06.Archived(PDF) from the original on Feb 17, 2024.
  8. ^"Using 'typeof' to infer a type".Learn TypeScript. Retrieved2022-01-28.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Typeof&oldid=1279864916"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp