What is difference Var and Dynamic in c#
Ans:
Many people have expressed confusion around the difference between var and dynamic in C#. For both of them, the type is inferred rather than explicitly declared.
dynamic test = 1;
var test2 = 2;
If I hover my mouse over the “var” in the code above, IntelliSense will show me that the compiler has correctly inferred that it is an Int32. If I hover over “dynamic”, it will continue to be typed as “dynamic” since dynamic types aren’t resolved until runtime.
However, var is statically typed, and dynamic is not.
// Can a dynamic change type?
dynamic test = 1;
test = "i'm a string now"; // compiles and runs just fine
var test2 = 2;
test2 = "i'm a string now"; // will give compile error
This is one of the key differences between dynamic and var. A var is an implicitly typed variable that is inferred by the compiler, but it is just as strongly typed as if you had explicitly typed it yourself using “int test2 = 2;”. A dynamic variable bypasses all compile-time type checking and resolves everything at runtime.
I’ll comment out the last line in the code above to get the code to compile, and add some code to verify the types of the variables.
// Can a dynamic change type?
dynamic test = 1;
Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);
test = "i'm a string now"; // compiles and run just fine
Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);
var test2 = 2;
//test2 = "i'm a string now"; // will give compile error
Console.WriteLine("Var as " + test2.GetType() + ": " + test2);
This produces the following output:
Dynamic as System.Int32: 1
Dynamic as System.String: i'm a string now
Var as System.Int32: 2

Question: Why we Used Dynamic ?
Ans: It is a new type that avoids compile time type checking.
Ans:
Many people have expressed confusion around the difference between var and dynamic in C#. For both of them, the type is inferred rather than explicitly declared.
dynamic test = 1;
var test2 = 2;
If I hover my mouse over the “var” in the code above, IntelliSense will show me that the compiler has correctly inferred that it is an Int32. If I hover over “dynamic”, it will continue to be typed as “dynamic” since dynamic types aren’t resolved until runtime.
However, var is statically typed, and dynamic is not.
// Can a dynamic change type?
dynamic test = 1;
test = "i'm a string now"; // compiles and runs just fine
var test2 = 2;
test2 = "i'm a string now"; // will give compile error
This is one of the key differences between dynamic and var. A var is an implicitly typed variable that is inferred by the compiler, but it is just as strongly typed as if you had explicitly typed it yourself using “int test2 = 2;”. A dynamic variable bypasses all compile-time type checking and resolves everything at runtime.
I’ll comment out the last line in the code above to get the code to compile, and add some code to verify the types of the variables.
// Can a dynamic change type?
dynamic test = 1;
Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);
test = "i'm a string now"; // compiles and run just fine
Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);
var test2 = 2;
//test2 = "i'm a string now"; // will give compile error
Console.WriteLine("Var as " + test2.GetType() + ": " + test2);
This produces the following output:
Dynamic as System.Int32: 1
Dynamic as System.String: i'm a string now
Var as System.Int32: 2

Question: Why we Used Dynamic ?
Ans: It is a new type that avoids compile time type checking.
A dynamic type escapes type checking at compile time; instead, it resolves type at run time.
A dynamic type can be defined using the
Difference between reflection and dynamic type.
dynamic
keyword.Difference between reflection and dynamic type.
Reflection | Dynamic | |
Inspect (meta-data) | Yes | No |
Invoke public members | Yes | Yes |
Invoke private members | Yes | No |
Caching | No | Yes |
Static class | Yes | No |
There are 4 great differences between Dynamic and reflection. Below is a detailed explanation of the same. Reference http://www.codeproject.com/Articles/593881/What-is-the-difference-between-Reflection-and-Dyna
Point 1. Inspect VS Invoke
Reflection can do two things one is it can inspect meta-data and second it also has the ability to invoke methods on runtime.While in Dynamic we can only invoke methods. So if i am creating software's like visual studio IDE then reflection is the way to go. If i just want dynamic invocation from the my c# code, dynamic is the best option.

Point 2. Private Vs Public Invoke
You can not invoke private methods using dynamic. In reflection its possible to invoke private methods.
Point 3. Caching
Dynamic uses reflection internally and it also adds caching benefits. So if you want to just invoke a object dynamically then Dynamic is the best as you get performance benefits.
Point 4. Static classes
Dynamic is instance specific: You don't have access to static members; you have to use Reflection in those scenarios.
Below are some differences between var and dynamic keyword in C#:
Var | Dynamic |
---|---|
It is introduced in C# 3.0. | It is introduced in C# 4.0 |
The variables are declared using var keyword are statically typed. | The variables are declared using dynamic keyword are dynamically typed. |
The type of the variable is decided by the compiler at compile time. | The type of the variable is decided by the compiler at run time. |
The variable of this type should be initialized at the time of declaration. So that the compiler will decide the type of the variable according to the value it initialized. | The variable of this type need not be initialized at the time of declaration. Because the compiler does not know the type of the variable at compile time. |
If the variable does not initialized it throw an error. | If the variable does not initialized it will not throw an error. |
It support intelliSense in visual studio. | It does not support intelliSense in visual studio |
var myvalue = 10; // statement 1 myvalue = “GeeksforGeeks”; // statement 2 Here the compiler will throw an error because the compiler has already decided the type of the myvalue variable using statement 1 that is an integer type. When you try to assign a string to myvalue variable, then the compiler will give an error because it violating safety rule type. | dynamic myvalue = 10; // statement 1 myvalue = “GeeksforGeeks”; // statement 2 Here, the compiler will not throw an error though the type of the myvalue is an integer. When you assign a string to myvalue it recreates the type of the myvalue and accepts string without any error. |
It cannot be used for properties or returning values from the function. It can only used as a local variable in function. | It can be used for properties or returning values from the function. |