Skip to content

Object and Dyn

The object Type

A type-safe universal GC reference that can hold any GC reference or auto-boxed value type:

object o = new Person("Alice"); // GC ref stored directly
object n = 42; // int auto-boxed
object f = 3.14; // float auto-boxed
object s = "hello"; // string auto-boxed

Type Checking and Unboxing

Use is and as to inspect and extract the underlying value:

object o = 42;
if (o is int)
{
int val = o as int; // unboxes to 42
}
if (o is Person p)
{
p.Greet(); // pattern binding with unbox
}

The dyn Type

A dynamic dispatch type that resolves field access and method calls at runtime:

dyn d = new Person("Alice");
int age = d.age; // runtime field lookup
d.age = 30; // runtime field write
d.Greet(); // runtime method resolution + call

If a field or method is not found at runtime, an exception is raised.

Auto-Boxing

Value types are automatically boxed when assigned to object or dyn:

Source TypeBox Type
int / int64__Box_i64
int32__Box_i32
int16__Box_i16
int8__Box_i8
float / float64__Box_f64
float32__Box_f32
bool__Box_bool
string__Box_string

object vs dyn

Featureobjectdyn
Holds any typeYesYes
Auto-boxingYesYes
Type-safe accessVia is/asNo (runtime only)
Field access by nameNoYes
Method call by nameNoYes
Runtime overheadMinimalLookup per access