Home > DeveloperSection > Forums > what is different Var And Dynamic Types In c#.
Ankit Singh

Total Post:341

Points:2389
Posted on    January-24-2016 10:32 PM

 C# C#  .NET 
Ratings:


 2 Reply(s)
 224  View(s)
Rate this:
I want to know what is different Var And Dynamic Types In c#. give me a sample example 


Shiva Shukla
Shiva Shukla

Total Post:13

Points:91
Posted on    January-24-2016 10:45 PM

Var is a type of declaration which is resolved at compile time that means compiler takes care of the type which is declared as var. The var keyword is used to declare the var type.

Dynamic is also a type of declaration which is decided at Run time. It means to say that if you declare a type of variable then compiler will check it on the Rum Time only. The dynamic keyword is used to declare dynamic type.
As you know it will check at rum time, so there is no need to initialize the value at compile time, you can initialize it on run time. 


Modified On Jan-24-2016 10:48:10 PM

aditya kumar Patel

Total Post:244

Points:1736
Posted on    January-24-2016 10:52 PM

Var

 Var keyword is an implicit way of defining DataTypes. Implicit means indirect way of defining variable types.
In simple words var keyword  data types is defined by the compiler during the generation of the “IL” code.

using System;
namespace ConsoleDemo

    public class Program
    {
        public static void Main(string[] args)
        {     

           var name = "Pass string data type ";
            name = 45545454;
            Console.WriteLine("The value of name is " + name);
            Console.ReadLine();
        }
    }
}

 

dynamic

Dynamic is a type of declaration This means the type of variable declared is decided by the compiler at runtime time.

 

using System;
namespace ConsoleDemo

    public class Program
    {
        public static void Main(string[] args)
        {

            dynamic data = "Pass string data type ";
            data = 88582045785;
            Console.WriteLine("The value of data is " + data);
            Console.ReadLine();
        }
    }
}

 

 


 

 


Modified On Jan-24-2016 11:00:53 PM

Don't want to miss updates? Please click the below button!

Follow MindStick