Introduction
This blog describes the Difference between var & dynamic keyword in C#.
var
- Statically typed - var type of variables are required to be initialized at the time of declaration. Once assigned a value into var type then it act as that data type like int,string,etc. So after that we can't able to change variable datatype.
dynamic
- Dynamically typed - Dynamically we can change the variable type like int,string,etc. That means the variable type declared at runtime.
Code
- using System;
- public class Program
- {
- public static void Main()
- {
- var result1 = 5;
- //result1 ="Hello World!!"; implicitly convert type 'string' to 'int'
- dynamic result2 = 6;
- result2 = "Welcome to C# Corner";
- Console.WriteLine(result1);// var keyword output
- Console.WriteLine(result2);// dynamic keyword output
- }
- }
5
Welcome to C# Corner
Welcome to C# Corner


Rathrola Prem KumarPosted Mar 19, 2017, 5:35 PM EDT
Clear and simple :) ....
SubashPosted Mar 18, 2017, 6:53 AM EDT
New for me thank you
Ritesh SinghPosted Jul 28, 2016, 5:11 AM EDT
Nice
Bhuvan PandeyPosted Jul 28, 2016, 4:16 AM EDT
Good work.
Tarun DPosted Jul 27, 2016, 10:14 PM EDT
Thanks for sharing...