Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Difference Between var and dynamic in C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (109 votes)
17 Jun 2013CPOL2 min read 372.4K   69   50
Difference between var and dynamic in C#

Introduction

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.

Background

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. This code snippet of two statements -

{

var obj1=1;

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. This code snippet of two statements -

{

dynamic obj1=1;

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.

History

  • 17th September, 2012: Submitted the tip and trick

License

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


Written By
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.

Interests:
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

 
SuggestionWe can pass as dynamic as a parameter in a method Pin
Adityakumar231829-May-18 1:12
Adityakumar231829-May-18 1:12 
QuestionNice Explanation! Pin
ItzDennisThomas14-Feb-18 21:55
ItzDennisThomas14-Feb-18 21:55 
Suggestiondouble declaration in last row of table? Pin
fastal22-Mar-17 5:23
fastal22-Mar-17 5:23 
GeneralSame point repeated Pin
jha.amit666629-Mar-16 20:46
jha.amit666629-Mar-16 20:46 
QuestionDynamic variables Pin
Meer Wajeed Ali20-Jul-15 1:11
professionalMeer Wajeed Ali20-Jul-15 1:11 
QuestionPeople who use either of these constructs (var / dynamic) represent the lowest common denominator Pin
Stephen Punak22-May-15 7:45
Stephen Punak22-May-15 7:45 
QuestionDynamically typed - This means the type of variable declared is decided by the compiler at runtime time?? Pin
ganesh.rit22-Jan-15 18:51
ganesh.rit22-Jan-15 18:51 
QuestionNice and Simple. Thanks Pin
Dilip Mevada8-Dec-14 4:36
Dilip Mevada8-Dec-14 4:36 
Generalnice and simple... 5! Pin
Sharath C V10-Nov-14 20:48
professionalSharath C V10-Nov-14 20:48 
voted 5...
SuggestionUsage Pin
ArchAngel12329-Oct-14 5:56
ArchAngel12329-Oct-14 5:56 
QuestionTwo very important points are missed here Pin
Rounak Hasan15-May-14 2:22
Rounak Hasan15-May-14 2:22 
AnswerRe: Two very important points are missed here Pin
SeanO715-Aug-16 22:00
SeanO715-Aug-16 22:00 
AnswerRe: Two very important points are missed here Pin
SeanO715-Aug-16 22:11
SeanO715-Aug-16 22:11 
GeneralMy vote of 3 Pin
yrajeshkumar16-Dec-13 1:25
yrajeshkumar16-Dec-13 1:25 
Suggestionvar v/s dynamic Pin
DoshiRavi7-Sep-13 5:56
DoshiRavi7-Sep-13 5:56 
GeneralRe: var v/s dynamic Pin
bbirajdar13-Oct-13 17:50
bbirajdar13-Oct-13 17:50 
GeneralRe: var v/s dynamic Pin
nirman b doshi7-Nov-17 22:55
nirman b doshi7-Nov-17 22:55 
Questionthanks for sharing Pin
Jagz W20-Jun-13 1:11
professionalJagz W20-Jun-13 1:11 
GeneralMy vote of 4 Pin
odedskal18-Jun-13 20:50
odedskal18-Jun-13 20:50 
GeneralMy vote of 4 Pin
Member 1010270718-Jun-13 18:18
Member 1010270718-Jun-13 18:18 
GeneralMy vote of 5 Pin
Member 1007181718-Jun-13 15:32
Member 1007181718-Jun-13 15:32 
GeneralMy vote of 5 Pin
Suresh Priyadarshana18-Jun-13 6:34
Suresh Priyadarshana18-Jun-13 6:34 
GeneralMy vote of 4 Pin
chait30118-Jun-13 4:45
chait30118-Jun-13 4:45 
GeneralMy vote of 3 Pin
RajasekaranCodeProject18-Jun-13 1:21
RajasekaranCodeProject18-Jun-13 1:21 
GeneralRe: My vote of 3 Pin
Ivandro Ismael24-May-14 6:21
Ivandro Ismael24-May-14 6:21 

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

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