What is the difference between var and dynamic Keyword in C#

No.of Views1960
Bookmarked0
By RRaveen  On 10 Feb 2011
emailbookmarkadd commentsprint
 

Answer:

var

If you are use the var keyword, then it valid type against right side value on declaration time

Example:

var data = "name";            
Console.WriteLine(data);

Here var will be string type at compile time.

dynamic

If you are use the dynamic keyword, the type will be decide on run time.

Example:

dynamic data = "name";
data = 8;
Console.WriteLine(data);

it will compile without any error, at run time data variable type is a int than string.because in the second line of code, you have change to string to in value on right hand side.

 
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popular Interview Questions On CSharp
^ Scroll to Top