Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between Var and Dynamic type in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.49k
    Comment on it

    Difference between Var and Dynamic type in C#

     

    Var Type

     

    • C# 3.0 introduced var type.

     

    • With respect to storage it can store any type of value with the condition that var type variables need to be initialized at time of declaration.

     

    • The information related to value stored in var type is known to compiler therefore it is type safe.

     

    • Var type work is restricted to the scope in which it is defined. Var type can neither be passed as method argument nor can be returned by methods.

     

    • Since compiler has all information about stored value in var type,it does not causes any problem.

     

    • Var type find it's usage in the scenario where type is anonymous i.e actual type is not known.

     

    Dynamic Type

     

    • C# 4.0 introduced dynamic type.

     

    • With respect to storage it can store any type of value. It is not mandatory to initialize dynamic type variables at time of declaration.

     

    • The information related to dynamic type variable is not known to compiler therefore it is not type safe.

     

    • Dynamic type can be passed as method arguments as well as can be returned by methods.

     

    • It may or may not cause problem as information about stored value in dynamic type is resolved at run time. The problem occur if wrong properties or methods are accessed by dynamic type variable.

     

    • Dynamic type find it's usage in the scenario where coding is to be done using reflection or dynamic languages or with the COM objects. 

 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: