Casts

The cast<T>(value) expression performs an explicit conversion to a selected destination type.

Integer conversions

Use a cast when changing integer width or signedness. The destination type controls truncation, extension and signed interpretation.

long large = 1000;
int compact = cast<int>(large);

uint unsignedValue = 42;
int signedValue = cast<int>(unsignedValue);

Character and integer conversions

char never converts implicitly to or from an integer. Use an explicit cast to expose a scalar value or to construct a character in low-level decoding code.

char emoji = '😀';
uint scalar = cast<uint>(emoji); // 0x1F600
char decoded = cast<char>(scalar);

A compile-time integer-to-char cast is rejected when the value is negative, greater than U+10FFFF or in U+D800–U+DFFF. A runtime cast follows Xenon's low-level integer conversion model and does not add an exception or runtime scalar check; the programmer must validate decoder input before casting.

Integer and floating-point conversions

Casts convert numeric values rather than reinterpreting their underlying bit pattern.

float ratio = 12.75f;
int whole = cast<int>(ratio);

int count = 8;
double precise = cast<double>(count);
Explicit intent

A cast makes width, signedness and numeric-domain changes visible at the point where they occur.

Enums and integers

Enum conversions are explicit: use cast<int>(State.Running) to obtain an integer and cast<State>(value) to construct an enum value from an integer. The latter does not check whether a named enum member exists for that value.

Casts involving nint, nuint, clong and culong use the selected target's widths, including inside enum initializers and switch labels. See target-dependent constants.

User-defined conversions

When no built-in cast applies, cast<T>(value) considers operator explicit and operator implicit declared in the source or destination struct. A cast may select at most one user conversion, with standard conversions before and after its call. See conversion operators for declaration syntax, overload ranking and pointer/reference restrictions.