Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Var vs Dynamic Keywords in C#.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 171
    Comment on it

    Var vs Dynamic Keywords in C#

    1) Introduction of these keywords

    • var was introduced in C# 3.0.
    • dynamic was introduced in C# 4.0.

    2) Variable initialization

    • When a variable is declared as var type then the variable is to be initialized at the time of declaration and if not initialized they encounter the compile time error i.e implicitly-typed local variables must be initialized. 
    • When a variable is declared as dynamic type  then variable need not to be initialized at the time of declaration.

    3) Type inference of variables

    • var type variables are compile time variables i.e the type of these variable are inferred at compile time. The type is determined by type of value that these variables are initialized with. In short var variables are statically typed variables.
    • Dynamic type variable are run time variable i.e the type of these variable are inferred at run time. In short dynamic variables are dynamically typed variables.
       

    4) Changing type of value assigned

    • var does not allow the type of value assigned to be changed after it is assigned to at the time of declaration i.e type of value cannot be changed onced assigned.
    • Dynamic allow the type of value assigned to the variable to be changed after it is assigned with initial value.

    5) Intellisense help

    • The type of var type variables are inferred at compile time as a result, the compiler has all the information related to the type therefore Intellisense help is available.
    • Dynamic type variables type is not known until run time therefore Intellisense help is not available.
       

    6) Restrictions on the usage

    • Creating properties and returning values from a function are usage of dynamic variables.
    • var variables can only be used as local variable in a function. These variables are not used for creating properties and returning values from a function.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: