Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

typeof: practical reference

typeof: practical reference: typeof returns a Type object for a compile-time-known type without constructing an instance.

Version: C# operator reference; verified against C# 14

Syntax

typeof(type)

typeof returns a Type object for a compile-time-known type without constructing an instance.

Use it for metadata, generic type definitions, and type comparisons.

An open generic uses typeof(List<>), and nullable-reference annotations do not create another Type.

typeof(dynamic) is compile-time error CS1962; use typeof(object) only when object is actually the intended runtime type.

The complete program actually uses typeof; its exact expected stdout is System.Int32.

Examples

using System;
using System.Linq;
using System.Threading.Tasks;

Console.WriteLine(typeof(int).FullName);

Result

System.Int32

The complete program actually uses `typeof`; its exact expected stdout is `System.Int32`.

Sources