C# Feature – dynamic
Overview
Section titled “Overview”dynamic
defers member binding to runtime, useful for interop or meta-programming, at the cost of type safety.
Introduced In
Section titled “Introduced In”C# 4 (2010)
Before
Section titled “Before”object x = GetComObject();// Reflection or explicit casts required
dynamic x = GetComObject();x.Save(); // resolved at runtime
Annotated Example
Section titled “Annotated Example”dynamic json = System.Text.Json.JsonSerializer.Deserialize<dynamic>("{\"a\":1}");Console.WriteLine(json.a);
Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Avoid in core domain logic; prefer static types.
- Exceptions surface at runtime if members are missing.
Related Features
Section titled “Related Features”Version Notes
Section titled “Version Notes”- Interacts with nullable contexts; prefer static typing where feasible.