Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between VAR and DYNAMIC keywords in C#?
The var and dynamic keywords in C# both allow you to declare variables without explicitly specifying their type, but they work very differently. The key difference is when type checking occurs ? var is resolved at compile-time, while dynamic is resolved at runtime.
Syntax
Following is the syntax for declaring a var variable −
var variableName = value;
Following is the syntax for declaring a dynamic variable −
dynamic variableName = value;
Using var Keyword
The var keyword creates statically typed variables. The compiler determines the type based on the assigned value at compile-time, and the type cannot change afterward.
Example
using System;
class Program {
static void Main(string[] args) {
var myInt = 5;
var myString = "Amit";
var myDouble = 3.14;
Console.WriteLine("Integer: {0}", myInt);
Console.WriteLine("String: {0}", myString);
Console.WriteLine("Double: {0}", myDouble);
Console.WriteLine("Type of myInt: {0}", myInt.GetType());
Console.WriteLine("Type of myString: {0}", myString.GetType());
}
}
The output of the above code is −
Integer: 5 String: Amit Double: 3.14 Type of myInt: System.Int32 Type of myString: System.String
Using dynamic Keyword
The dynamic keyword creates dynamically typed variables. Type checking happens at runtime, and the same variable can hold different types during execution.
Example
using System;
class Program {
static void Main(string[] args) {
dynamic val = 100;
Console.WriteLine("Value: {0}, Type: {1}", val, val.GetType());
val = "Hello Dynamic";
Console.WriteLine("Value: {0}, Type: {1}", val, val.GetType());
val = 3.14;
Console.WriteLine("Value: {0}, Type: {1}", val, val.GetType());
val = true;
Console.WriteLine("Value: {0}, Type: {1}", val, val.GetType());
}
}
The output of the above code is −
Value: 100, Type: System.Int32 Value: Hello Dynamic, Type: System.String Value: 3.14, Type: System.Double Value: True, Type: System.Boolean
Key Differences Comparison
| Feature | var | dynamic |
|---|---|---|
| Type Resolution | Compile-time | Runtime |
| Type Safety | Statically typed | Dynamically typed |
| Performance | Fast (no runtime overhead) | Slower (runtime type checking) |
| Type Change | Cannot change after declaration | Can change during execution |
| IntelliSense Support | Full support | Limited support |
| Initial Value | Must be initialized | Can be declared without initialization |
Error Handling Differences
Example
using System;
class Program {
static void Main(string[] args) {
// var example - compile-time error prevention
var varNumber = 10;
Console.WriteLine("var number: " + varNumber);
// dynamic example - runtime flexibility
dynamic dynValue = 10;
Console.WriteLine("dynamic as int: " + dynValue);
dynValue = "Now I'm a string";
Console.WriteLine("dynamic as string: " + dynValue);
// This would cause a runtime error if uncommented:
// dynValue = 10;
// int result = dynValue.NonExistentMethod(); // Runtime error
Console.WriteLine("Program completed successfully");
}
}
The output of the above code is −
var number: 10 dynamic as int: 10 dynamic as string: Now I'm a string Program completed successfully
Conclusion
Use var for compile-time type inference when you want static typing with convenience. Use dynamic when you need runtime flexibility, such as working with COM objects or dynamic languages. var is generally preferred for performance and type safety.
