Javatpoint Logo
Javatpoint Logo

Difference between Var and Dynamic in C#

In this article, we will discuss the differences between Var and Dynamic keywords. Before discussing their differences, we must know about the Var and Dynamic in C#. As we know, the beginning of programming in any language is marked by the declaration of variables, their logical definition, and consequent implementation. Therefore, before we start writing code, it is important to know how to declare variables in the programming language.

As with previous versions of C#, all code written is self-validated at compile time. It is a statically typed language where variables are declared using the var keyword. After C# version 4.0 and later introduced "dynamic", where syntax validation or error checking occurs only at runtime. The keyword "Dynamic" has also been introduced for variable declarations.

What is the Var Keyword?

An implicitly typed local variable (var) is a variable that is declared without explicitly specifying a .NET type. When a variable is implicitly typed, it will automatically determine its type by using the value provided during initialization at compile time. C# 3.0 introduces the idea of implicitly typed variables. Implicitly typed variables are not intended to replace regular variable declarations. Rather, it is intended to handle specific cases like LINQ (Language Integrated Query).

Syntax:

It has the following syntax:

Example:

Let us take an example to illustrate the Var keyword in C#.

Output:

The type of 'a1' is System.Char 
The type of 'a2' is System.String 
The type of 'a3' is System.Double 
The type of 'a4' is System.Boolean 
The type of 'a5' is System.Int32 

Explanation:

In this example, the program displays the use of the Var keyword. It allows the user to declare variables without defining the datatype. The compiler can automatically assign the data type. The variables are declared, and the program uses the console statement to display the type of the var keyword.

What is the Dynamic Keyword?

A new dynamic keyword was introduced in C# 4.0. The type of variables declared dynamic was dynamically expressed. These variable types are declared and determined at runtime. Declared dynamic variables do not require initialization at the time of declaration. The compiler does not know about the variable time at compile time, so it cannot catch the error at compile time. At runtime, the variable type is set, resulting in the absence of IntelliSense.

Similar to C#'s var keyword, the dynamic keyword is also useful for defining variables without specifying an explicit type. At compile time, the Var variable is set to its type, while at run time, it is fixed to a Dynamic Variable.

Example 1:

Example 2:

When an integer is assigned to ob, the compiler does not generate any error message. The compiler creates the type of obj as a string, and again, it recreates the type of obj as the integer type when assigning an integer value to ob.

Example:

Let us take an example to illustrate the Dynamic Keyword in C#.

Output:

The actual datatype of v1: System.String
The actual datatype of v2: System.Int32
The actual datatype of v3: System.Double
The actual datatype of v4: System.Boolean

Explanation:

In this example, the dynamic keyword is used for creating variables in which their datatypes are defined at runtime instead of compile time. In the above program, v1, v2, v3, and v4 are declared using the dynamic keyword by using the GetType() method. The result is converted to a string using the ToString() method.

Main Differences between Var and Dynamic in C#

Difference between Var and Dynamic in C#

There are several differences between var and dynamic in C#. Some main differences are as follows:

Var Dynamic
Var in the C# language was introduced in C#3.0 Dynamics was introduced later in C#4.0
Variables are declared using the statically typed var keyword. Variables are declared using the dynamic keyword, which is dynamically typed.
Variables are assigned to the compiler for interpretation at compilation. The compiler decides the type of variables during runtime.
This variable type must be initialized at declaration time. The variable is assigned a type by the compiler, which takes into use and its initialization. This variable type does not need to be initialized at declaration time. During compilation, the variable's type is unknown to the compiler.
If the variable is not initialized, an error is thrown. If the variable is not initialized, no error occurs.
IntelliSense is supported in Visual Studio. Visual Studio doesn't support IntelliSense.
variable temp = 100;
temp = "Program";
The compiler returns an error because the type of the temporary variable was already determined by statement 1, and it is of type integer. The compiler displays a warning when you assign a string to a 'temporary variable', which implies that security measures cannot be violated.
dynamic temp = 100;
temp = "Programming";
The compiler does not return an error even if temp is of type integer.
Assigning a string to "temp" recreates the type "temp" and accepts the string successfully.

Conclusion:

In this article, we have learned about the Var and Dynamic keywords in C#. Both are used for different purposes in variable declarations. We can choose these keywords according to the program's requirements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA