Click here to Skip to main content
Click here to Skip to main content

Tagged as

Difference between var and dynamic in C#

, 17 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Difference between var and dynamic in C#.


The type keyword 'var' was introduced in C# 3.0 (.NET 3.5 with Visual Studio 2008) and the type 'dynamic' was introduced in C# 4.0 ( .NET 4.0 with Visual Studio 2010). Let us see the difference between these two


Variables declared with var are implicitly but statically typed. Variables declared with dynamic are dynamically typed. This capability was added to the CLR in order to support dynamic languages like Ruby and Python.

This means that dynamic declarations are resolved at run-time, var declarations are resolved at compile-time. 

Table of difference 

var dynamic

Introduced in C# 3.0

Introduced in C# 4.0

Statically typed – This means the type of variable declared is decided by the compiler at compile time.

Dynamically typed - This means the type of variable declared is decided by the compiler at runtime time.

Need to initialize at the time of declaration.

e.g., var str=”I am a string”;

Looking at the value assigned to the variable str, the compiler will treat the variable str as string.

No need to initialize at the time of declaration.

e.g., dynamic str; 

str=”I am a string”; //Works fine and compiles

str=2; //Works fine and compiles

Errors are caught at compile time.

Since the compiler knows about the type and the methods and properties of the type at the compile time itself

Errors are caught at runtime 

Since the compiler comes to about the type and the methods and properties of the type at the run time.

Visual Studio shows intellisense since the type of variable assigned is known to compiler.

Intellisense is not available since the type and its related methods and properties can be known at run time only


e.g., var obj1;

will  throw a compile error since the variable is not initialized. The compiler needs that this variable should be initialized so that it can infer a type from the value.

e.g., dynamic obj1; 

will compile;

e.g. var obj1=1;

will compile 

var obj1=” I am a string”;

will throw error since the compiler has already decided that the type of obj1 is System.Int32 when the value 1 was assigned to it. Now assigning a string value to it violates the type safety.

e.g. dynamic obj1=1;

will compile and run 

dynamic obj1=” I am a string”; 

will compile and run since the compiler creates the type for obj1 as System.Int32 and then recreates the type as string when the value “I am a string” was assigned to it.

This code will work fine. 


  • Submitted the tip and trick - 17 Sep 2012.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

aspnet -i
Web Developer
India India
Software developer by profession, working for a service and product based organisation in India.
Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.
I love reading the blogs and articles of technology experts. I love codeproject and stackoverflow .
I love to share knowledge and help the programmers. I appreciate if some body corrects my code or my concepts which helps me learn.

Comments and Discussions

SuggestionUsage PinmemberArchAngel12329-Oct-14 5:56 
QuestionTwo very important points are missed here Pinmemberhasan.rounak15-May-14 2:22 
GeneralMy vote of 3 Pinmemberyrajeshkumar16-Dec-13 1:25 
Suggestionvar v/s dynamic PinmemberDoshiRavi7-Sep-13 5:56 
GeneralRe: var v/s dynamic Pinmember aspnet -i13-Oct-13 17:50 
Questionthanks for sharing PinmemberJagz W20-Jun-13 1:11 
GeneralMy vote of 4 Pinmemberodedskal18-Jun-13 20:50 
GeneralMy vote of 4 PinmemberMember 1010270718-Jun-13 18:18 
GeneralMy vote of 5 PinmemberMember 1007181718-Jun-13 15:32 
GeneralMy vote of 5 Pinmembersuresh lasantha18-Jun-13 6:34 
GeneralMy vote of 4 PinmemberMember 1011024018-Jun-13 4:45 
GeneralMy vote of 3 PinmemberRajasekaranCodeProject18-Jun-13 1:21 
GeneralRe: My vote of 3 PinmemberIvandro Ismael24-May-14 6:21 
GeneralMy vote of 5 PinmemberChandrasekara Reddy17-Jun-13 20:27 
QuestionDifference between var and dynamic in C# PinmemberJamsheed Rahman17-Jun-13 18:14 
GeneralMy vote of 5 PinmemberJayanta Chatterjee17-Jun-13 4:55 
GeneralMy vote of 5 Pinmemberjohannesnestler17-Jun-13 4:07 
GeneralRe: My vote of 5 Pinmemberaspnet_regiis -u17-Jun-13 4:21 
QuestionI voted 5 PinmemberAnotherKen12-Jun-13 15:45 
AnswerRe: I voted 5 Pinmemberaspnet_regiis -u12-Jun-13 21:10 
GeneralMy vote of 5 PinmemberLoRdPMN12-Jun-13 6:50 
GeneralRe: My vote of 5 Pinmemberaspnet_regiis -u12-Jun-13 8:57 
GeneralGood Comparison Pinprofessionalrajneeshrai10-Jun-13 20:29 
GeneralRe: Good Comparison Pinmemberaspnet_regiis -u12-Jun-13 8:58 
GeneralMy vote of 5 PinprofessionalRenju Vinod29-May-13 0:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141030.1 | Last Updated 17 Jun 2013
Article Copyright 2012 by aspnet -i
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid