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;
-
-
- dynamic result2 = 6;
-
- result2 = "Welcome to C# Corner";
-
- Console.WriteLine(result1);
-
- Console.WriteLine(result2);
- }
- }
Output
5
Welcome to C# Corner
Demo : https://dotnetfiddle.net/PHCM3m