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, 9:35 PM
Clear and simple :) ....
SubashPosted Mar 18, 2017, 10:53 AM
New for me thank you
Ritesh SinghPosted Jul 28, 2016, 9:11 AM
Nice
Bhuvan PandeyPosted Jul 28, 2016, 8:16 AM
Good work.
Tarun DPosted Jul 28, 2016, 2:14 AM
Thanks for sharing...