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#.

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

History

  • Submitted the tip and trick - 17 Sep 2012.

License

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

Share

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

 
QuestionNice and Simple. Thanks PinmemberMember 18810678-Dec-14 5:36 
Generalnice and simple... 5! PinprofessionalSharath C V10-Nov-14 21:48 
SuggestionUsage PinmemberArchAngel12329-Oct-14 6:56 
QuestionTwo very important points are missed here Pinmemberhasan.rounak15-May-14 3:22 
GeneralMy vote of 3 Pinmemberyrajeshkumar16-Dec-13 2:25 
Suggestionvar v/s dynamic PinmemberDoshiRavi7-Sep-13 6:56 
GeneralRe: var v/s dynamic Pinmember aspnet -i13-Oct-13 18:50 
Questionthanks for sharing PinmemberJagz W20-Jun-13 2:11 
GeneralMy vote of 4 Pinmemberodedskal18-Jun-13 21:50 
GeneralMy vote of 4 PinmemberMember 1010270718-Jun-13 19:18 
GeneralMy vote of 5 PinmemberMember 1007181718-Jun-13 16:32 
GeneralMy vote of 5 Pinmembersuresh lasantha18-Jun-13 7:34 
GeneralMy vote of 4 PinmemberMember 1011024018-Jun-13 5:45 
GeneralMy vote of 3 PinmemberRajasekaranCodeProject18-Jun-13 2:21 
GeneralRe: My vote of 3 PinmemberIvandro Ismael24-May-14 7:21 
GeneralMy vote of 5 PinmemberChandrasekara Reddy17-Jun-13 21:27 
QuestionDifference between var and dynamic in C# PinmemberJamsheed Rahman17-Jun-13 19:14 
GeneralMy vote of 5 PinmemberJayanta Chatterjee17-Jun-13 5:55 
GeneralMy vote of 5 Pinmemberjohannesnestler17-Jun-13 5:07 
GeneralRe: My vote of 5 Pinmemberaspnet_regiis -u17-Jun-13 5:21 
QuestionI voted 5 PinmemberAnotherKen12-Jun-13 16:45 
AnswerRe: I voted 5 Pinmemberaspnet_regiis -u12-Jun-13 22:10 
GeneralMy vote of 5 PinmemberLoRdPMN12-Jun-13 7:50 
GeneralRe: My vote of 5 Pinmemberaspnet_regiis -u12-Jun-13 9:57 
GeneralGood Comparison Pinprofessionalrajneeshrai10-Jun-13 21:29 
GeneralRe: Good Comparison Pinmemberaspnet_regiis -u12-Jun-13 9:58 
GeneralMy vote of 5 PinprofessionalRenju Vinod29-May-13 1:05 
GeneralRe: My vote of 5 Pinmemberaspnet_regiis -u12-Jun-13 9:58 
GeneralMy vote of 3 PinmemberThanasis Ioannidis17-Sep-12 12:03 
QuestionNot exactly right PinmemberThanasis Ioannidis17-Sep-12 12:02 
SuggestionMy vote of 3 PinmemberHeWillem17-Sep-12 3:46 
GeneralRe: My vote of 3 Pinmemberaspnet_regiis17-Sep-12 4:03 
AnswerRe: My vote of 3 PinmemberChristian Amado17-Sep-12 4:15 
GeneralRe: My vote of 3 PinmemberLev Pavlas17-Sep-12 8:06 
GeneralRe: My vote of 3 PinmemberHeWillem26-Sep-12 5:40 
GeneralRe: My vote of 3 Pinmemberaspnet_regiis26-Sep-12 6:38 
GeneralRe: My vote of 3 Pinmemberjohannesnestler17-Jun-13 5:26 
GeneralMy vote of 5 PinmemberChristian Amado17-Sep-12 3:23 
GeneralRe: My vote of 5 Pinmemberaspnet_regiis17-Sep-12 4:04 

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
Web01 | 2.8.141220.1 | Last Updated 17 Jun 2013
Article Copyright 2012 by aspnet -i
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid