The dynamic Type
Defer type resolution to runtime with dynamic.
The dynamic Type is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is dynamic?
The dynamic type bypasses compile-time type checking. Member access and operations are resolved at runtime instead.
using System;
class Program {
static void Main() {
dynamic value = 10;
Console.WriteLine(value + 5);
}
}Reassigning Different Types
A dynamic variable can hold any type and even change type across assignments.
using System;
class Program {
static void Main() {
dynamic x = 1;
Console.WriteLine(x);
x = "now a string";
Console.WriteLine(x);
}
}Runtime Binding
With dynamic, the compiler trusts you. Method and property lookups happen when the line runs, not when it compiles.
using System;
class Program {
static void Main() {
dynamic text = "hello";
Console.WriteLine(text.ToUpper());
}
}Errors Surface at Runtime
Because checking is deferred, a wrong member throws a RuntimeBinderException at runtime rather than failing to compile.
using System;
using Microsoft.CSharp.RuntimeBinder;
class Program {
static void Main() {
dynamic n = 5;
try {
n.NoSuchMethod();
} catch (RuntimeBinderException) {
Console.WriteLine("No such member");
}
}
}dynamic vs var
var is fixed at compile time; dynamic stays flexible at runtime. They look similar but behave very differently.
using System;
class Program {
static void Main() {
var fixedType = 10;
dynamic flexible = 10;
flexible = "changed";
Console.WriteLine(fixedType + " " + flexible);
}
}Arithmetic on dynamic
Operators on dynamic are resolved at runtime based on the actual values involved.
using System;
class Program {
static void Main() {
dynamic a = 3;
dynamic b = 4;
Console.WriteLine(a * b);
}
}dynamic Return Values
A method can return dynamic when the type genuinely varies, but callers lose compile-time help.
using System;
class Program {
static dynamic GetValue(bool flag) {
return flag ? (dynamic)42 : "text";
}
static void Main() {
Console.WriteLine(GetValue(true));
Console.WriteLine(GetValue(false));
}
}Use Cases for dynamic
dynamic helps with COM interop, JSON-like data, and calling code where the shape is unknown until runtime.
using System;
class Program {
static void Main() {
dynamic data = 100;
data += 1;
Console.WriteLine(data);
}
}Performance Cost
Runtime binding has overhead. For tight loops or simple code, prefer static types for speed and safety.
using System;
class Program {
static void Main() {
int fast = 0;
for (int i = 0; i < 5; i++) fast += i;
Console.WriteLine(fast);
}
}No IntelliSense
Editors cannot suggest members on a dynamic, since the type is unknown until runtime. This slows development.
using System;
class Program {
static void Main() {
dynamic s = "Code";
Console.WriteLine(s.Length);
}
}Use Sparingly
Reach for dynamic only when static typing truly cannot express the scenario; otherwise prefer concrete types.
using System;
class Program {
static void Main() {
dynamic flexible = 7;
Console.WriteLine(flexible.GetType().Name);
}
}Quick Check
Test your understanding of dynamic.
Recap
dynamic turns off compile-time type checking and resolves members at runtime, so a dynamic variable can change type and invalid calls throw RuntimeBinderException.
Unlike var (fixed at compile time), dynamic is flexible but costs performance, loses IntelliSense, and should be used sparingly.
Frequently asked questions
Is the “The dynamic Type” lesson free?
Yes — the full text of “The dynamic Type” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “The dynamic Type”?
Defer type resolution to runtime with dynamic. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The dynamic Type” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.