
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is the difference between VAR and DYNAMIC keywords in C#?
Dynamic
Store any type of value in the dynamic data type variable created using dynamic keyword. Type checking for these types of variables takes place at run-time. Dynamic are dynamically typed variables.
The following is the syntax for declaring a dynamic type −
dynamic <variable_name> = value;
The following is an example −
dynamic val1 = 100; dynamic val2 = 5; dynamic val3 = 20;
The dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at runtime.
Var
The "var" keyword initializes variables with var support. Just assign whatever value you want for the variable, integer, string, float, etc. It is a statically typed variable.
Example
using System; namespace Demo { class Program { static void Main(string[] args) { var myInt = 5; var myString = "Amit"; Console.WriteLine("Rank: {0}
Name: {1}",myInt,myString); } } }
Output
Rank: 5 Name: Amit
- Related Articles
- Difference between var and dynamic in C#
- What is the difference between "var" and "val" in Kotlin?
- What is the difference between keywords and reserved words in Java?
- What is the difference between super and this, keywords in Java?
- What is the difference between throw and throws keywords in Java?
- What is the difference between keywords const and readonly in C#?
- What is the difference between static and dynamic polymorphism?
- What is the difference between: var functionName = function() {} and function functionName() {} in Javascript
- What is the difference between public, static and void keywords in C#?
- Difference between var and let in JavaScript
- Difference between Var and Dynamics in C#
- Explain the difference between let and var in Swift
- What is the difference between dynamic type variables and object type variables?
- Difference Between extends and implements keywords in Java
- Explain the difference between const and readonly keywords in C#

Advertisements