Typeof
typeof(타입오브), typeOf, TypeOf는 여러 프로그래밍 언어에서 변수의 자료형을 알아내기 위해 제공하는 연산자이다. 자료형을 명시적으로 지정하지 않고 데이터의 여러 유형을 수용해야 하는 프로그램들을 구성할 때 유용하다.
예시
C (프로그래밍 언어)의 비표준(GNU) 확장에서 typeof는 두 변수의 최대 값을 결정할 목적으로 일반 매크로를 정의하기 위해 사용할 수 있다:
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
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) ); }
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)
function isNumber(n) { return ( typeof n === 'number' ); }
타입스크립트:[1]
function (param: typeof existingObject) { ... }
let newObject: typeof existingObject;
같이 보기
- sizeof
각주
- ↑ “Using `typeof` to infer a type”. 《Learn TypeScript》 (영어). 2022년 1월 28일에 확인함.