Home » Tutorials » C# » Difference between Var and Dynamic in c#

Difference between Var and Dynamic in c#

Isolation Levels in Sql Server
Constant vs ReadOnly in C#

Difference between Var and Dynamic in c#

Hello friend,Today I will explain about  Difference between Var and Dynamic in c# which is as below:

Var Dynamic
Introduced in C# 3.0 Introduced in C# 4.0
Statically typed variable, type of variable declared is decided by the compiler at compile time. Dynamically typed variable, type of variable declared is decided by the compiler at runtime.
IntelliSense help available, because Compiler know the variable type. IntelliSense help doesn’t  available,because type of variable would be known at runtime only.
Error caught at compile time. Error caught at runtime.
Could not change assigned variable’s type.
e.g. var obj=10;
will compile
obj=”I am a string”;
but if we assigned different type to same variable, if will give error at compile time only.
Could change assigned variable’s type.
e.g. dynamic obj=10;
will compile
obj=”I am a string”;
Variable must be assigned any value at initialization.
e.g. var obj;
will give error at compile time only
It’s not mandatory to assign value to variable at initialization
e.g dynamic obj;
will compile successfully.
var variables cannot be used for property or return values from a function. They can only be used as local variable in a function. dynamic variables can be used to create properties and return values from a function

About

Hello friends, My self Dhruv Sheth, I have more than 6 years of Industrial experience as a Software Developer. In my career I got chance to work with asp.net, c#,Web API, JavaScript, JQuery, WPF, WCF , Windows Application, PostScript, vb.net, GhostScript etc. So I have decided to share my knowledge with everyone and finally I have come up with TechnoThirsty.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload CAPTCHA.




DISCLAIMER:
The content is copyrighted to technothirsty.com and may not be reproduced on other websites without permission from the owner.You may contact us using the information below.