Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Difference Between VB.NET and C#
Next article icon

Difference between var and dynamic in C#

Last Updated : 30 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
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#: 

VarDynamic
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.

Next Article
Difference Between VB.NET and C#
author
ankita_saini
Improve
Article Tags :
  • Difference Between
  • C#
  • CSharp-keyword

Similar Reads

  • 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 p
    2 min read
  • Difference Between VB.NET and C#
    Visual Basic .NET is a high-level programming language that was initially developed in 1991. It was the first programming language that directly supported programming graphical user interfaces using language-supplied objects. It supports all the concepts of an object-oriented such as object, class,
    2 min read
  • Difference between Static and Dynamic Hazard
    Hazard: In digital circuits, specially in combinational circuits, they give rise to behavior's, glitches and/or wrong output signals. While designing electronics circuits, one must be in a position to distinguish between the static hazards and dynamic hazards. Interference or interruption occurs whe
    4 min read
  • Difference between SRAM and DRAM
    SRAM and DRAM are two types of RAMs. SRAM i.e., static RAM is RAM in which data is stored in transistors in the form of voltage. DRAM i.e., dynamic RAM is RAM in which data is stored in capacitors in the form of electric charges. The SRAM is faster and expensive whereas DRAM is slower and less expen
    5 min read
  • Difference between String and string in C#
    String is an alias for System.String class and instead of writing System.String one can use String which is a shorthand for System.String class and is defined in the .NET base class library. The size of the String object in memory is 2GB and this is an immutable object, we can not modify the charact
    2 min read
  • Difference between Static and Dynamic IP address
    As you discuss connectivity of devices to the internet, or communication within a network, an IP address is taken as a basic understanding. There are two main types of IP addresses: Static and Dynamic. The purpose of this article is to describe what IP addresses are and the difference between static
    7 min read
  • Difference Between Static and Dynamic Web Pages
    There are two basic methods of web design: static and dynamic web pages. Users access static web pages, which present the same content every time they are viewed. On the other hand, dynamic webpages create content instantly in response to user input and present customized or updated information. Let
    3 min read
  • Difference Between List and Set in C#
    The list is C# is the same as the list in JAVA. Basically, it is a type of object which can store variables. But in difference with objects, it stores the variables only in a specific order. Following is the syntax from which we can declare variables: Syntax: List<int> numbers = new List<in
    2 min read
  • Difference Between VB.NET and Java
    Visual Basic .NET is a high-level programming language that was initially developed in 1991 run on .NET platforms. It was the first programming language that directly supported programming graphical user interfaces using language-supplied objects. It supports all the concepts of an object-oriented s
    2 min read
  • 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
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences