Programming languages can normally be considered to be either statically typed or dynamically typed. A static (not to be confused with the static keyword, used for classes) typed language validates the syntax or checks for any errors during the compilation of the code. On the other hand, dynamically typed languages validate the syntax or check for errors only at run time. For example, C# and Java are a static type and JavaScript is a dynamically typed language.
C# was previously considered to be a statically typed language since all the code written was validated at the compile time itself. But, with the introduction of the dynamic keyword in C# 4.0, it became a dynamically typed language. The two concepts of static and dynamic types in C# can be illustrated with the use of the two keywords named var and dynamic. Let's discuss some basic differences between these two, based on some of their characteristics.
When were they introduced
- var was introduced in C# 3.0
- dynamic was introduced in C# 4.0
Type inference of variables
- var is a statically typed variable. It results in a strongly typed variable, in other words, the data type of these variables is inferred at compile time. This is done based on the type of value that these variables are initialized with.
- dynamic are dynamically typed variables. This means, their type is inferred at run-time and not the compile time in contrast to var type.
Initialization of variables
- var type of variables are required to be initialized at the time of declaration or else they encounter the compile time error: Implicitly-typed local variables must be initialized.
- dynamic type variables need not be initialized when declared.
Changing the type of value assigned
- var does not allow the type of value assigned to be changed after it is assigned to. This means that if we assign an integer value to a var then we cannot assign a string value to it. This is because, on assigning the integer value, it will be treated as an integer type thereafter. So thereafter no other type of value cannot be assigned. For example, the following code will give a compile time error.

- dynamic allows the type of value to change after it is assigned initially. In the code above, if we use dynamic instead of var, it will not only compile but will also work at run-time. This is because, at run time, the value of the variable is first inferred as Int32 and when its value is changed, it is inferred to be a string type.

Intellisense help
- Intellisense help is available for the var type of variables. This is because, its type is inferred by the compiler from the type of value it is assigned and as a result, the compiler has all the information related to the type. So we have the Intellisense help available. See the code below. We have a var variable initialized with a string value. So its type is known to the compiler and we have the appropriate Intellisense help available for it.

- Intellisense help is not available for dynamic type of variables since their type is unknown until run time. So IntelliSense help is not available. Even if you are informed by the compiler as "This operation will be resolved at run-time". See the code below.

Restrictions on the usage
- dynamic variables can be used to create properties and return values from a function.
- var variables cannot be used for property or return values from a function. They can only be used as local variables in a function.
var vs dynamic vs object
If we closely observe the dynamic type, it is performing pretty much the same task that the object type (which is the base type of all other types) does. Then what is the difference between an object type and a var then? Also, why do we need a var when we have the object type? Let's discuss these points by doing some comparisons.
- When using an object type, the compiler provides only generic information, functionality, or functions related to the type it holds, until it is being typecast into its actual type. This means that even if we have stored integer or string values in an object type, we will get their related functions, only if we convert the object type into its actual type. See the code below.

- Here, we only have generic properties and functions available for the type of value stored in it, that we will have, even if we store an integer or other type of value in it. Now, let's convert it to its actual type, string.

- Now after explicit conversion, we have the properties and functions specific to the string type. In case we use the var type to store the same value, we would not be required to do the explicit conversion, since the compiler already infers its type from the value initialized and we will have its functions and properties.
- Based on the preceding point, object types increase the overhead of boxing and unboxing, before we can use the actual values stored in it or use any function related to it. But this is not the case with var, since we already know its type at the time of use. For example, in the code above, we get to use the functions related to a string only after we convert the object into a string. So this results in un-boxing. On the other hand, for dynamic types, we only need to know that the function or property we are using actually exists for the type of value being stored in it.

- For example, in the code above, if we know that the dynamic variable will be storing a string type, then we can use the Length type property, even if it is not available in the IntelliSense help.
- Next, we need to be careful when casting or converting the values, when using the dynamic or object type variables. Any incorrect casting can result in runtime errors. On the other hand, a var type will give a compile time error for an incorrect conversion. So a run time error is not possible.

- Here, the object type is stored as a string value, but we are trying to cast it into an integer type. So it will throw a run time error.

- Similarly, we are storing back the string value, in an integer type. So it would again result in an error.
- In terms of interoperability with various frameworks, earlier we required an object type to be used to get the underlying object type and then use reflection to access the related methods or functions. But with the introduction of the dynamic keyword, we only need to know that a function or property exists on the underlying object, and for the rest, we simply need to make a call to these properties or functions.
- As per MSDN, "As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.".
From these points, we cannot conclude which specific type we should use. It all depends on the requirements of whether we should go for a var dynamic or object type.
So this was all about the basic differences between var and dynamic keywords. I hope you enjoyed reading it.
JayavarapuPosted Jan 18, 2024, 6:24 AM
Like the way you explained.
Praneet RanePosted Mar 20, 2020, 10:46 AM
If someone want very clear understanding of the concept and why it was introduced?, then this is the article to refer. Thanks for sharing!
Praneet RanePosted Mar 20, 2020, 10:44 AM
If someone want very clear understanding of the concept, this is the post. Thanks for sharing!
Dharmendra Kumar PanditPosted Jan 23, 2020, 11:24 PM
Nice sharig......
Dinesh GabhanePosted Nov 13, 2019, 12:41 AM
Thanks for sharing!
ganesh mandlikPosted Sep 27, 2019, 1:20 AM
Perfectly Explained, Thanks for sharing!
Hammond WenPosted Jul 7, 2019, 8:34 AM
"As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time." Could you explain more detail about this?
Hammond WenPosted Jul 7, 2019, 8:33 AM
Great article! Thank you.
Osvaldo TomazPosted Jun 21, 2019, 9:21 AM
Hello guys and fellows programmers! I have a doubt about the way var works behind the scenes. Example: "var myFloat = 2.0f; var myDouble = 2.0; var myDecimal = 2.0m; ". That was easy to understand, but how var works in this case : "var myNumber = 120; " can't be a long because has no L, people says: integer, but by the value a "byte" would be much more better because less bytes ... My doubt is there ... Var goes to apply a variable with less bytes or in that case var does not go to byte or sbyte, it always go to int ? If so , use explicit would be better because this number "120" fills perfect in a variable "byte" half of the bytes of an "int". Since now I already say thanks to all who can help me with this doubt about this partner called "var"
Dennis ThomasPosted Feb 15, 2018, 3:49 AM
Good one Jasminder.... Thank you!
Pradeep YadavPosted Feb 11, 2018, 11:19 AM
Well described.....
mohit SPosted Dec 18, 2017, 6:08 AM
V nicely written keep it up ..
Sunil PalPosted Apr 26, 2017, 1:36 AM
Thanks Paaji, All doubts got solved , this was asked in an interview to me !!
Sandeep KumarPosted Aug 24, 2016, 7:51 AM
Nice one.. But one doubt here when i did this var varvalue = "sandeep sharma"; int name = Convert.ToInt32(varvalue); .. did not get any complie time error...
Anil PomarPosted Jul 21, 2016, 11:27 PM
It was a Great Article with precise Examples ...............Appreciated your Efforts.
Dinu DivakaranPosted May 6, 2016, 2:23 AM
Great Article
Avinash PhadkePosted Mar 7, 2016, 3:55 AM
Jasminder it's very useful explanation of var and dynamic datatype. It really helps me to choose correct datatype and help me to overcome the boxing and un-boxing in application
K P Singh ChundawatPosted Jan 20, 2016, 5:38 AM
Thank you Jasminder Singh ... Finally My Concept is cleared . Thanks for sharing with good explanation .
Rohan RewalePosted Jan 8, 2016, 4:55 AM
Very nice explanation in simple language
Yashwanth MuthineniPosted Aug 19, 2015, 6:11 AM
Nice Share
kewal sharmaPosted May 15, 2015, 2:56 AM
Good One
Surya KantPosted Apr 17, 2015, 2:19 AM
Thanks Jasminder for sharing such type of Valuable information..It's really help me in understanding var and dyanamic.
Jasminder SinghPosted May 5, 2014, 10:39 AM
@Pankaj, Object is completely different from the dynamic. Object is the base of all other data types. To be more precise, an Object can be used to store int, string or any other class type. But to get the value back into actual class type, you need boxing and un-boxing....Dynamic is just another way to achieve what dynamic provides, without need of boxing or un-boxing.
Pankaj BajajPosted May 5, 2014, 1:37 AM
Thanks Jasminder for sharing good information..As per article it looks that Object and Dynamic are same. Is this true or there is difference between them?
Sam HobbsPosted Mar 5, 2014, 1:50 PM
Also, JavaScript cannot be statically typed because it is not compiled. Is there any language that is compiled yet is exclusively dynamically typed?
Sam HobbsPosted Mar 5, 2014, 1:45 PM
The dynamic type was designed for unmanaged (COM) objects, correct? I think that should be mentioned. There is probably no need for the dynamic type for managed objects except maybe for advanced applications.
Jasminder SinghPosted Mar 5, 2014, 9:35 AM
@Mehmet, could you provide some details about ur requirement or issue u r getting...??
Jasminder SinghPosted Mar 5, 2014, 9:34 AM
@Jeetendra, Thank you Jeetendra ji...!!!
Mehmet AktasPosted Mar 5, 2014, 2:47 AM
foreach my hand found how do I implement a dynamic list? I have been receiving run time error. thanks
Jeetendra GundPosted Mar 5, 2014, 2:04 AM
Good One ..Jasminder...