Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • What is the difference between var and dynamic keywords in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 349
    Comment on it

    Programming languages can be categorized into two parts based on data type

    1) Strongly Type
    2) Weakly Type

    Strongly Type:-
           A strongly-typed programming language is one in which there are different datatypes(char,int,float) defined in programming language and all the variables, constants declared must be from one of the type. Certain operations can be performed only with certain data types. The language compiler enforces the data type at compile time. C#, JAVA are the example of strongly type languages.

    Weakly Type:-
           A weakly-typed programming language is one in which there is no separate data types defined in the programming language.Types are inferred at run time. Java Script and Perl are the examples of weakly type languages.

    C# was previously considered to be as strongly type language because type of the data was known at compile time but due to introduction of dynamic keyword in 4.0 it has become weakly type language also. Let's discuss some basic differences between these two, based on some of their characteristics.

    1) Version of introduction:-
    var:- C# 3.0
    dynamic:- C# 4.0

    2) Type inference of variables:-
       var is a strongly typed variable. Its datatype is inferred at compile time. This is done based on the type of value that these variables are initialized with.
       dynamic are dynamically typed variables. This means, their type is inferred at run-time and not the compile time

       var type of variables are required to be initialized at the time of declaration otherwise compiler would not compile and would show the error.
       dynamic type variables need not be initialized when they are declared like var keyword.

    4)Changing type of value assigned:-
       var-: Any variable declared with var keyword need to be initialized with some value. Based on that value compiler would inferred the type of variable but once initialized then we can't change the type of value.

    var obj1=100;//will compile    
    obj1="This is test string";

    This will throw error since the compiler has already decided that the type of obj1 is System.Int32 when the value 100 was assigned to it. Now assigning a string value to it violates the type safety.
    As the type of date in inferred at run time we change the type of data.

    dynamic obj1=100;//will compile and run 
    dynamic obj1="I am a test string"; 

    This will compile and run since the compiler creates the type for obj1 as System.Int32 and then recreates the type as string when the value I am a string was assigned to it. This code will work fine.

       Intellisense help is available for variable declared with var keyword because type of data is known at compile time but for dynamic keyword Intellisense is not supported because type of data is not known until runtime

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: