Compare keywords Var and Dynamic in C#

Tram Ho

1. Introduction of Static typing and Dynamic typing

Common programming languages ​​can be divided into Static typing and Dynamic typing. Static typing (not to be confused with the static keyword, used for classes) validates the syntax or checks for errors during compilation. Dynamic typing-type languages, on the other hand, validate the syntax or check for errors at runtime. For example, C # and Java are Static typing and JavaScript is a Dynamic typing language.

C # was previously considered a Static typing language, as all written code is validated at compile time. But with the introduction of the Dynamic keyword in C # 4.0, it has also become a Dynamic typing language. The concepts of Static and Dynamic typing in C # can be illustrated using two keywords named Var and Dynamic.

2. Introduction to keywords Var and Dynamic

  • Var first appeared in C # 3.0 and Dynamic was introduced in C # 4.0
  1. The timing of the variable types infer the data type
  • Var is a variable that is typed statically and the data types of these variables are inferred at compile time. This is done based on the type of value to which these variables are initialized.
  • Dynamic variables are dynamically entered. This means that their data types are inferred at run-time, not compile-time against the Var type.
  1. Initialize variable types
  • With Var data types of variables must be initialized at the time of declaration, otherwise they will encounter errors at compile time “Implicitly-typed local variables must be initialized.”
  • With Dynamic, we do not need to initialize when declaring.
  1. Changes the data type of the variable that has been initialized
  • Var does not allow to change data type after it has been assigned. This means that if we assign an Int value to Var then we cannot assign a string value to it. This is because, when assigning an Int value, it is then treated as an int. Therefore, no other value type can then be assigned.
  • Dynamic allows the data type to change after it has been originally assigned. In the code above, if we use Dynamic instead of Var, it will not only compile but also run at run time. This is because, at runtime, the value of the first variable inferred is Int and when its value is changed, it is inferred as a string.
  1. Limitations of Var and Dynamic
  • Dynamic variables can be used to create properties and return values ​​from a function.
  • The Var variable cannot be used for property values ​​or returned from a function. They can only be used as local variables within a function.

3. Compare Var, Dynamic and Object

If we look closely at the type of Dynamic, it is doing pretty much the same thing as the Object type (which is the base type of all the other types). So what is the difference between Object and Var types? Also, why do we need Var when we have Object type. Let’s discuss these points by making some comparisons.

  1. When using an Object type, the compiler only provides general information, functions, or functions related to the type it holds, until it is imported its actual type. This means that even if we have stored values ​​of type Int or string in an Object type, we will only get their associated functions when we convert the object type to its actual type. . See the example below:

    Here, we only have properties and functions that are common to the declared value type even if we store an integer or other value type in it. Now, let’s convert it to its actual data type, string.

    Now after converting to the string type, we have its specific properties and functions. But if we use the Var type to store the same value, we won’t be forced to do an explicit conversion, as the compiler will infer its data type from the initialized value and them. we will have its functions and properties.

  2. The object type will have to combine boxing and unboxing if we want to use the real values ​​stored in it or use any of the functions associated with it. But with Var there is no need of that process because we already know its data type at the time of use. For example, in the above code, we can only use the string variable related functions after we convert the object to string. Therefore, this leads to the elimination of boxing. On the other hand, for Dynamic types, we just need to know that the function or property we are using actually exists for the data type being stored in it.
    For example, in the image below, if we declare a Dynamic variable to store a string value, we can use the Length property.

  3. We need to be careful when casting or converting values, when using Dynamic or Object variables. Any error can lead to an error at program runtime. Var type on the other hand will cause compile-time error for an incorrect conversion. So it is not possible to have errors during program runtime.

    As in the above example, the data type stored is string, but we are trying to cast the type to Int. So it will cause a runtime error in the program

    Similarly, if we are storing a string value and casting it to Int. So it will again result in an error.

4. Conclusion

Above is some basic information about 2 keywords Var and Dynamic and compare them with Object type. It cannot be compared to which one is better, depending on the circumstances and the requirements of the problem. Hope this article will help you to use these 2 keywords in the best way.

Share the news now

Source : Viblo