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).
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.
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#:
|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.|
- C# | Implicitly Typed Local Variables - var
- var keyword in C#
- Difference between Static and Dynamic SQL
- Difference between Static and Dynamic Routing
- Difference between Static and Dynamic Web Pages
- Difference between Static and Dynamic IP address
- Difference between Fixed and Dynamic Channel Allocations
- Difference between Basic Disk and Dynamic Disk
- Difference between Static and Dynamic Hazard
- Difference between Static and Dynamic Memory Allocation in C
- Differences between Dynamic Binding and Message Passing in Java
- Object and Dynamic Array in C#
- Dynamic Type in C#
- Greedy approach vs Dynamic programming
- C# Dynamic Coding - Attributes in Depth
- Difference and Similarities between PHP and C
- Difference between Stop and Wait protocol and Sliding Window protocol
- Similarities and Difference between Java and C++
- Difference between Yaacomo and and XAP
- Difference between VoIP and and POTS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vishalsingh80