Open In App

Difference between var and dynamic in C#

Last Updated : 21 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Implicitly Typed Local Variables – var are those variables which are declared without specifying the .NET type explicitly. In implicitly typed variable, the type of the variable is automatically deduced at compile time by the compiler from the value used to initialize the variable. The implicitly typed variable concept is introduced in C# 3.0. The implicitly typed variable is not designed to replace the normal variable declaration, it is designed to handle some special-case situation like LINQ(Language-Integrated Query). Example: 

CSharp




// C# program to illustrate the concept
// of the implicitly typed variable
using System;
 
class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Creating and initializing
        // implicitly typed variables
        // Using var keyword
        var a = 'f';
        var b = "GeeksforGeeks";
        var c = 30.67d;
        var d = false;
        var e = 54544;
 
        // Display the type
        Console.WriteLine("Type of 'a' is : {0} ", a.GetType());
 
        Console.WriteLine("Type of 'b' is : {0} ", b.GetType());
 
        Console.WriteLine("Type of 'c' is : {0} ", c.GetType());
 
        Console.WriteLine("Type of 'd' is : {0} ", d.GetType());
 
        Console.WriteLine("Type of 'e' is : {0} ", e.GetType());
    }
}


Output:

Type of 'a' is : System.Char 
Type of 'b' is : System.String 
Type of 'c' is : System.Double 
Type of 'd' is : System.Boolean 
Type of 'e' is : System.Int32 

In C# 4.0, a new type is introduced that is known as a dynamic type. It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time, instead of this, the compiler gets the type at the run time. The dynamic type variable is created using dynamic keyword. Example: 

CSharp




// C# program to illustrate how to get the
// actual type of the dynamic type variable
using System;
 
class GFG {
 
    // Main Method
    static public void Main()
    {
 
        // Dynamic variables
        dynamic val1 = "GeeksforGeeks";
        dynamic val2 = 3234;
        dynamic val3 = 32.55;
        dynamic val4 = true;
 
        // Get the actual type of
        // dynamic variables
        // Using GetType() method
        Console.WriteLine("Get the actual type of val1: {0}",
                                  val1.GetType().ToString());
 
        Console.WriteLine("Get the actual type of val2: {0}",
                                  val2.GetType().ToString());
 
        Console.WriteLine("Get the actual type of val3: {0}",
                                  val3.GetType().ToString());
 
        Console.WriteLine("Get the actual type of val4: {0}",
                                  val4.GetType().ToString());
    }
}


Output:

Get the actual type of val1: System.String
Get the actual type of val2: System.Int32
Get the actual type of val3: System.Double
Get the actual type of val4: System.Boolean

Below are some differences between var and dynamic keyword in C#: 

.var_vs_dynamic-table { border-collapse: collapse; width: 100%; } .var_vs_dynamic-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .var_vs_dynamic-table th { border: 1px solid #5fb962; padding: 8px; } .var_vs_dynamic-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .var_vs_dynamic-table tr:nth-child(odd) { background-color: #ffffff; } 

Var Dynamic
It is introduced in C# 3.0. It is introduced in C# 4.0
The variables are declared using var keyword are statically typed. The variables are declared using dynamic keyword are dynamically typed.
The type of the variable is decided by the compiler at compile time. The type of the variable is decided by the compiler at run time.
The variable of this type should be initialized at the time of declaration. So that the compiler will decide the type of the variable according to the value it initialized. The variable of this type need not be initialized at the time of declaration. Because the compiler does not know the type of the variable at compile time.
If the variable does not initialized it throw an error. If the variable does not initialized it will not throw an error.
It support intelliSense in visual studio. It does not support intelliSense in visual studio
var myvalue = 10; // statement 1 myvalue = “GeeksforGeeks”; // statement 2 Here the compiler will throw an error because the compiler has already decided the type of the myvalue variable using statement 1 that is an integer type. When you try to assign a string to myvalue variable, then the compiler will give an error because it violating safety rule type. dynamic myvalue = 10; // statement 1 myvalue = “GeeksforGeeks”; // statement 2 Here, the compiler will not throw an error though the type of the myvalue is an integer. When you assign a string to myvalue it recreates the type of the myvalue and accepts string without any error.
It cannot be used for properties or returning values from the function. It can only used as a local variable in function. It can be used for properties or returning values from the function.


Similar Reads

Difference between $var and $$var in PHP
In PHP, $var is used to store the value of the variable like Integer, String, boolean, character. $var is a variable and $$var stores the value of the variable inside it. $var: Syntax: $variable = value;The $variable is the variable nameThe value is the initial value of the variable. Example 1: This example stores and displays values with $. C/C++
2 min read
What Is the Difference Between VAR.S Function and VAR.P Function in Excel?
Functions in Excel are the pre-defined formulae used to obtain a certain value and to perform calculations using the specific value in a certain order. The basic functions are SUM, AVG, MAX, MIN, etc used for the range of cells. In order to obtain the correct results from the function, the correct syntax of the functions is required. In this articl
6 min read
var keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. var is a keyword, it is used to declare an implicit type variable, that specifies the type of a variable based on initial value. Syntax: var variable_name = value; Example: Input: a = 637 b = -9721087085262 Output: value of a 637, type
2 min read
C# | Implicitly Typed Local Variables - var
Implicitly typed variables are those variables that are declared without specifying the .NET type explicitly. In an implicitly typed variable, the type of the variable is automatically deduced at compile time by the compiler from the value used to initialize the variable. The implicitly typed variable concept is introduced in C# 3.0. The implicitly
4 min read
Difference between Static and Dynamic SQL
Static or Embedded SQL are SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application. Dynamic SQL is SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries. Dynamic SQL is a programming technique that enables you to build SQL
2 min read
Difference between Static and Dynamic Routing
Both Static routing and Dynamic routing are the Types of Routing. Static Routing: Static Routing is also known as non-adaptive routing which doesn't change the routing table unless the network administrator changes or modifies them manually. Static routing does not use complex routing algorithms and It provides high or more security than dynamic ro
2 min read
Difference between Static and Dynamic Web Pages
Static Web pages: Static Web pages are very simple. It is written in languages such as HTML, JavaScript, CSS, etc. For static web pages when a server receives a request for a web page, then the server sends the response to the client without doing any additional process. And these web pages are seen through a web browser. In static web pages, Pages
2 min read
Difference between Fixed and Dynamic Channel Allocations
Prerequisite - Channel Allocation Problem in Computer Network Fixed Channel Allocation (FCA): Fixed Channel Allocation is a strategy in which fixed number of channels or voice channels are allocated to the cells. Once the channels are allocated to the specific cells then they cannot be changed. In FCA channels are allocated in a manner that maximiz
2 min read
Difference between Basic Disk and Dynamic Disk
Basic Disk: A basic disk is a type of hard drive configuration, available with the Windows operating system. To manage all partitions and data on the hard disk, normal partition tables or logical drives are used. They are the storage types most often used with Windows. It can contain up to four primary partitions, or three primary partitions and an
3 min read
Difference between Static and Dynamic Hazard
1. Static Hazard : Static hazard occur in combinational circuits and can be eliminated by using redundant gates. Static hazard is further classified as: 1. Static-1 hazard 2. Static-0 hazard 2. Dynamic hazard : When the output changes several times then it should change from 1 to 0 or 0 to 1 only once, it is called dynamic hazard. Dynamic hazard oc
2 min read