What are dynamic data types in C#?

The dynamic data type in C# allows you to store any type of value in a variable. Unlike other data types, type checking for dynamic variables occurs at runtime rather than compile-time, providing flexibility but requiring careful handling to avoid runtime errors.

Syntax

Following is the syntax for declaring a dynamic variable −

dynamic <variable_name> = value;

Basic Dynamic Variable Declaration

Example

using System;

class Program {
   public static void Main() {
      dynamic val1 = 100;
      dynamic val2 = "Hello World";
      dynamic val3 = 15.5;
      dynamic val4 = true;
      
      Console.WriteLine("val1: " + val1 + " (Type: " + val1.GetType() + ")");
      Console.WriteLine("val2: " + val2 + " (Type: " + val2.GetType() + ")");
      Console.WriteLine("val3: " + val3 + " (Type: " + val3.GetType() + ")");
      Console.WriteLine("val4: " + val4 + " (Type: " + val4.GetType() + ")");
   }
}

The output of the above code is −

val1: 100 (Type: System.Int32)
val2: Hello World (Type: System.String)
val3: 15.5 (Type: System.Double)
val4: True (Type: System.Boolean)

Dynamic Type Flexibility

Dynamic variables can change their type during runtime, allowing operations that would be impossible with statically typed variables −

Example

using System;

class Program {
   public static void Main() {
      dynamic value = 10;
      Console.WriteLine("Initial value: " + value + " (Type: " + value.GetType() + ")");
      
      value = "Now I'm a string";
      Console.WriteLine("Changed value: " + value + " (Type: " + value.GetType() + ")");
      
      value = 25.75;
      Console.WriteLine("Changed again: " + value + " (Type: " + value.GetType() + ")");
      
      // Dynamic operations
      dynamic a = 5;
      dynamic b = 10;
      Console.WriteLine("Sum: " + (a + b));
   }
}

The output of the above code is −

Initial value: 10 (Type: System.Int32)
Changed value: Now I'm a string (Type: System.String)
Changed again: 25.75 (Type: System.Double)
Sum: 15

Dynamic vs Object Type

Dynamic vs Object Type Checking dynamic Type checking at Runtime No casting needed Direct method calls Runtime errors possible object Type checking at Compile-time Casting required Boxing/unboxing Compile-time safety

Example

using System;

class Program {
   public static void Main() {
      // Dynamic type
      dynamic dynamicVar = "Hello";
      Console.WriteLine("Dynamic: " + dynamicVar.ToUpper()); // Direct method call
      
      // Object type
      object objectVar = "Hello";
      Console.WriteLine("Object: " + ((string)objectVar).ToUpper()); // Casting required
      
      // Dynamic can change type
      dynamicVar = 42;
      Console.WriteLine("Dynamic changed: " + dynamicVar);
      
      // Object holds reference but needs casting
      objectVar = 42;
      Console.WriteLine("Object value: " + (int)objectVar);
   }
}

The output of the above code is −

Dynamic: HELLO
Object: HELLO
Dynamic changed: 42
Object value: 42

Comparison

Feature dynamic object
Type checking Runtime Compile-time
Casting required No Yes
Performance Slower (runtime resolution) Faster (compile-time resolution)
Error detection Runtime Compile-time

Common Use Cases

  • COM Interop − Working with COM objects where member information isn't available at compile-time.

  • JSON deserialization − Handling dynamic JSON structures without predefined classes.

  • Reflection scenarios − When types are determined at runtime and you need flexible method calls.

Conclusion

Dynamic data types in C# provide runtime flexibility by deferring type checking until execution. While they offer convenience for scenarios like COM interop and dynamic JSON handling, they sacrifice compile-time safety and performance. Use dynamic types judiciously when runtime flexibility outweighs the benefits of static typing.

Updated on: 2026-03-17T07:04:35+05:30

648 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements