Wednesday, February 11, 2026

Difference Between var and dynamic in C#

 Var: Var is implicitly typed
         The type is decided at compile time
         Once assigned, the type cannot change
         strongly typed. 
         intellisese worked properly.  
         Errors are checked at compile time

Ex:
var name = "Naresh";   // Compiler treats it as string
// name = 10; ❌ Error (cannot change type)


Dynamic: dynamice is resolved ar runtime
         type checking happens at runtime, not compile time
         the type can change
         no compile time type checking
         intellisense is limited.
         errors may occur at runtime
Ex: 
dynamic value = "Naresh";
value = 10;   // ✅ Allowed

value = true; // ✅ Allowed