C# Reference ยท Keywords and Statements
The dynamic type
Reference for C# dynamic: late-bound operations resolved at run time, the DLR, ExpandoObject, DynamicObject, errors, performance costs and when to avoid it.
dynamic tells the compiler to skip static type checking for any expression involving the variable. Member lookups, method calls, operators and conversions are resolved at run time, based on the actual type of the object, by the Dynamic Language Runtime (DLR). If the operation is invalid for that object, you get a RuntimeBinderException instead of a compile error.
It exists mainly for interop with COM objects, dynamic languages and loosely shaped data such as JSON-like trees. In ordinary code, static types are safer and faster.
dynamic x = "hello"; Console.WriteLine(x.Length); // bound at run time: 5
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
dynamic name = value; dynamic Method(dynamic arg) { ... } List<dynamic> items; Type t = typeof(object); // 'dynamic' is object at run time
| Concept | Description |
|---|---|
dynamic |
A static type that disables compile-time checking of operations on it. At run time it is stored as object. |
| Binding | Each dynamic operation is bound on first use using the runtime type, then cached at the call site. |
ExpandoObject |
A dictionary-backed object whose members you add by assigning. Also an IDictionary<string, object?>. |
DynamicObject |
A base class you override (TryGetMember, TrySetMember, TryInvokeMember, ...) to define your own dynamic behavior. |
Errors: unknown members or no matching overload raise RuntimeBinderException at run time. A dynamic expression is never checked by the compiler, so typos are found only when the line runs.
Usage notes
- Implicit conversion. Any type converts to
dynamicanddynamicconverts to any type; the second conversion is checked at run time. - Overload resolution at run time. If an argument is dynamic, the whole call is bound at run time with the arguments' actual types. A
staticmethod call with a dynamic argument returnsdynamic. - No extension methods and no IntelliSense. A call on a dynamic receiver cannot find extension methods, including LINQ:
d.Sum()on aList<int>throws at run time ('List<int>' does not contain a definition for 'Sum'). Cast first:((IEnumerable<int>)d).Sum(). - Performance. The first call at a site is slow (it starts the binder and may compile an expression tree); later calls with the same types are cached but still slower than static calls. Keep dynamic code out of hot loops.
- Anonymous types. An anonymous object created in another assembly is internal, so reading its properties through
dynamicthrows aRuntimeBinderException. - COM interop. Office automation was a main motivation: methods with
objectparameters acceptdynamic. - Alternatives. Prefer interfaces, generics, pattern matching, or a typed
Dictionary<string, object>for loose data; useSystem.Text.Json.Nodes.JsonObjectfor JSON trees. - Not related to
var.varis inferred at compile time;dynamicis resolved at run time.
Related
- var
- typeof and sizeof
- is and as
- Tutorial: Reflection, Type conversion