A variable declared as dynamic skips compile-time type checking. The compiler accepts any member access, method call or operator on it and records what you asked for; the runtime works out at execution time whether the operation is valid for the object the variable actually holds. The keyword was added in C# 4.0, mainly to make COM interop and dynamic languages such as IronPython easier to call.
Example
dynamic value = "This is a dynamic type";
Console.WriteLine(value.GetType()); // System.String
Console.WriteLine(value.Length); // 22
value = 42;
Console.WriteLine(value.GetType()); // System.Int32
Console.WriteLine(value + 8); // 50
The same variable holds a string and then an integer, and value.Length and value + 8 are both resolved at run time against whatever is in it at that moment.
Errors move to run time
Because the compiler does not check anything, a typo compiles without complaint:
dynamic value = "hello";
Console.WriteLine(value.Lenght);
This throws Microsoft.CSharp.RuntimeBinder.RuntimeBinderException with the message that string does not contain a definition for Lenght. You also lose IntelliSense, refactoring support and “find all references” on anything typed as dynamic. The result of most operations on a dynamic value is itself dynamic, so the lack of checking spreads to every variable declared with var from it.
dynamic, var and object
These three are easy to mix up:
| Declaration | Type known at compile time | Member access checked |
|---|---|---|
var x = "text"; | Yes, string (inferred) | At compile time |
object x = "text"; | Yes, object | At compile time, so x.Length is an error without a cast |
dynamic x = "text"; | No | At run time |
var is pure syntax: the variable is fully, statically typed. dynamic is the only one that changes behavior. Under the hood, a dynamic variable is stored as object, and the compiler generates call-site objects that perform the binding at run time and cache the result.
Where dynamic still earns its place
COM interop. Office automation APIs return object everywhere. With dynamic, sheet.Cells[1, 1].Value = "Name" works without a cast at each step. This is Windows-only.
ExpandoObject. System.Dynamic.ExpandoObject lets you add members at run time, which is handy for building ad-hoc objects to pass to a template engine or serializer:
using System.Dynamic;
dynamic person = new ExpandoObject();
person.Name = "Ada";
person.Born = 1815;
Console.WriteLine(person.Name); // Ada
var members = (IDictionary<string, object?>)person;
Console.WriteLine(members.Count); // 2
An ExpandoObject is a dictionary underneath, so you can cast it to IDictionary<string, object?> to enumerate or remove members.
Interop with dynamic languages and some older libraries. Newtonsoft.Json’s JObject supports dynamic access, for example dynamic json = JObject.Parse(text); string name = json.name;. System.Text.Json does not; use JsonNode there (node["name"]) or, better, deserialize into a class.
Why to avoid it in most code
Performance. The first call at each call site is slow, because the runtime binder has to analyse the operation. Later calls with the same types are cached and fast, but still slower than a normal call.
Trimming and Native AOT. The runtime binder relies on reflection and is not compatible with trimming. Code that uses dynamic produces warnings when you publish trimmed, and does not work with Native AOT.
Extension methods do not work. A call like value.Where(...) on a dynamic receiver fails at run time, because the binder does not know which using directives were in scope. Cast to the real type first, or call the extension as a static method.
Anonymous types across assemblies. Anonymous types are internal. Passing one as dynamic to code in another assembly and reading its properties throws RuntimeBinderException.
If you find yourself using dynamic to call different methods based on an object’s runtime type, a switch expression with type patterns or an interface usually does the job with full compile-time checking.