C# Difference between Var and Dynamic with Examples

  By : Suresh Dasari
  Posted On : 02-May-2023

Here we will learn what is var in c#, var example in c#, what is dynamic keyword in c#, dynamic example in c#, and the differences between var and dynamic in c# with examples.

What is Var in C#?

In c#, var keyword is useful to define the variables without specifying the explicit type. Based on the initialized value, the compiler will determine the variable's type.

 

While defining the variables with the var keyword, you need to make sure that variables are declared and initialized in the same statement otherwise you will get a compile-time error.

 

Following is the example of defining and initializing the variables with var keyword in c#.

 

var x = 10; // int type
var y = "Welcome"; // string type
var z = 10.30; // double type

The var variables will not accept null value and you are not allowed to use var variables as method parameters. To learn more about the var keyword, check Var Keyword in C# with Examples

What is Dynamic in C#?

Same as var keyword in c#, the dynamic keyword is also useful to define the variables without specifying the explicit type. The only difference between var and dynamic is the type of var variable is determined during the compilation time and the type of dynamic variable is determined during the runtime.

 

Following is the example of defining and initializing the dynamic variables in c# using the dynamic keyword.

 

dynamic x = 10; // int type
dynamic y = "Welcome"; // string type
dynamic z = 10.30; // double type

While defining the methods, you can use dynamic type as a method parameter and it will accept any type of parameter.

 

To learn more about the dynamic keyword, visit Dynamic Keyword in C# with Examples.

Difference between Var and Dynamic in C#

As discussed, the var and dynamic keyword is useful to define the variables without specifying the explicit type. The following table lists the main differences between var and dynamic keywords in c#.

 

VarDynamic
The var keyword is useful to define the variables without specifying explicit type. The dynamic keyword is same as var keyword to define the variables without specifying the type.
The type of var variables will be determined based on the assigned value during compile time. The type of dynamic variables will be determined during the runtime time.
While creating the var variables, we need to make sure that the variable is declared and initialized in the same statement. It's not the case with dynamic variables. You can define the variable and assign the value whenever it is required.
The var variables will not accept the null values. You can assign the null values to the dynamic variables.
You are not allowed to define the var type as a method parameter. You can use dynamic type as a method parameter and it will accept any type of value.
You are not allowed to initialize the multiple var variables in the same statement. You can initialize the multiple dynamic variables in the same statement.

To learn more about var, dynamic keyword, and other c# topics, visit our C# Tutorial.