赛派号

2025款汉兰达最新款报价多少 typeof

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 he 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] or D[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 he other constructs for obtaining run-time type information, such as typeid.

Examples[edit] C[edit]

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 (differentiating it from decltype in C++).[6][7]

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; }) C++[edit]

In C++, while there is no typeof operator, there is a decltype operator, which can be used to represent the type of a variable. Although C23 added typeof, it does not exist in C++. Unlike C23 typeof, decltype does not strip const-volatile-reference and atomic qualification.

struct A { double x; } const A* a; // decltype can be used in type declarations decltype(a->x) y; // equivalent to double y; // decltype can be used in trailing return types // This may be necessary for overloaded operators where, for instance, // type T + type U may result in type V distinct from T and U template auto add(T t, U u) -> decltype(t + u) { return t + u; } C#[edit]

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); } Ja[edit]

Ja does not he 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.

String s = "Hello, world!"; Class sClass = s.getClass(); Class stringClass = String.class; Class intClass = int.class; Object obj = /* something here */; System.out.printf("The class of %s is %s%n", obj, obj.getClass().getName()); JaScript[edit]

In JaScript:

function isNumber(n) { return ( typeof n === 'number' ); } TypeScript[edit]

In TypeScript:[8]

function (param: typeof existingObject) { ... } let newObject: typeof existingObject; Python[edit]

Python he the built-in function type.[9]

>>> type(123) VB.NET[edit]

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[edit] sizeof decltype Type introspection References[edit] ^ "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. ^ "Built-in Functions". Python documentation. Retrieved 20 June 2025.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了