Dynamic Binding in C#

In Dynamic binding, the compiler defers type checking until runtime instead of compile time. This allows you to work with objects whose types are determined dynamically, providing flexibility when dealing with anonymous types, COM objects, or dynamic languages.

The dynamic keyword in C# enables late binding, where method calls, property access, and type conversions are resolved at runtime. This is particularly useful for returning anonymous types from methods, since anonymous type names are only visible to the compiler.

Syntax

Following is the syntax for declaring a dynamic variable −

dynamic variableName = value;

Following is the syntax for a method returning dynamic type −

public dynamic MethodName() {
   return someObject;
}

Using Dynamic with Anonymous Types

One common use of dynamic is to return anonymous types from methods, since you cannot explicitly declare the anonymous type as a return type −

using System;

class Program {
   public static dynamic GetStudentInfo() {
      return new {
         StudentName = "Tom",
         Subject = "C#",
         Grade = 85
      };
   }

   public static void Main() {
      dynamic student = GetStudentInfo();
      Console.WriteLine("Student: " + student.StudentName);
      Console.WriteLine("Subject: " + student.Subject);
      Console.WriteLine("Grade: " + student.Grade);
   }
}

The output of the above code is −

Student: Tom
Subject: C#
Grade: 85

Dynamic vs Static Typing

The following example demonstrates the difference between static and dynamic typing −

using System;

class Program {
   public static void Main() {
      // Static typing - type checked at compile time
      string staticVar = "Hello";
      Console.WriteLine("Static: " + staticVar.Length);

      // Dynamic typing - type checked at runtime
      dynamic dynamicVar = "Hello";
      Console.WriteLine("Dynamic: " + dynamicVar.Length);

      // Change type at runtime
      dynamicVar = 42;
      Console.WriteLine("Dynamic changed: " + dynamicVar);
   }
}

The output of the above code is −

Static: 5
Dynamic: 5
Dynamic changed: 42

Runtime Type Resolution

With dynamic binding, method calls and property access are resolved at runtime based on the actual type of the object −

using System;

class Calculator {
   public int Add(int a, int b) {
      return a + b;
   }

   public double Add(double a, double b) {
      return a + b;
   }
}

class Program {
   public static void Main() {
      dynamic calc = new Calculator();
      
      // Method resolution happens at runtime
      dynamic result1 = calc.Add(5, 10);        // calls Add(int, int)
      dynamic result2 = calc.Add(5.5, 10.5);    // calls Add(double, double)
      
      Console.WriteLine("Integer result: " + result1);
      Console.WriteLine("Double result: " + result2);
   }
}

The output of the above code is −

Integer result: 15
Double result: 16

Dynamic vs Static Comparison

Static Typing Dynamic Typing
Type checking at compile time Type checking at runtime
Better performance Slight performance overhead
IntelliSense support No IntelliSense support
Compile-time error detection Runtime error detection

Conclusion

Dynamic binding in C# provides runtime type resolution using the dynamic keyword. While it offers flexibility for scenarios like anonymous types and interoperability, it comes with performance overhead and loss of compile-time type safety.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements