Var is a used as a variable in C#.It is Compile time variable and the type of variable is decided at Compile time. It is introduced in C#3.0 and no need to specify the datatype for variable ,it is implicitly converted into the desired datatype. It is required variable initialization if it is not initialized then compiler will throw an exception.Since it is Compile time so it supports the intellisense.We can not use Var as a return value from any Method and we can not pass Var as a parameter in Method Call.
Dynamic is a used as a variable in C#.It is Run time variable and the type of variable is decided at Runtime. It is introduced in C#4.0 and no need to specify the datatype for variable ,it is implicitly converted into the desired datatype. It is does not required any variable initialization if it is not initialized then compiler will not throw an exception.Since it is Runtime so it does supports the intellisense.We can use Dynamic as a return value from any Method and we can pass Dynamic as a parameter in Method Call.
Below is the Tabular Representation of the Differences in Var and Dynamic.
Sr.No | Var | Dynamic |
---|---|---|
1 | It uses Var Keyword | It Uses Dynamic Keyword |
2 | It uses Compile Time Error Checking | It uses Runtime Error Checking |
3 | It was introduced in C# 3.0 | It was introduced in C# 4.0 |
4 | It required Variable Initialization before use | It is not required Initialization of Variable |
5 | It supports Intellisense | It does not support Intellisense |
6 | It does not used as a Return Type in Method | Can used as a Return Type in Method |
7 | Can not pass as a Parameter in Method Call | Can pass as a Parameter in Method Call |
8 | It provides implicitly type conversion | Dynamic is not required the type converstion |
About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the
applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server,
EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.