Differences between Object, Var and Dynamic type

Differences between Object, Var and Dynamic type

23 May 2024
Intermediate
207K Views
6 min read
Learn with an interactive course and practical hands-on labs

Free C# Foundation Course: Learn C# In 21 Days

Implicit Types in C#: An Overview

C# is rich in data type. C# provides several different ways to represent variable types such as Object, Var, and Dynamic to store data of any type. In this C# Tutorial, we will explore more about Implicit Types which will include object vs var vs dynamic, differences between object, var, and dynamic in c#, and, when to use var object and dynamic.

What are Object, Var, and Dynamic type

Object:

  • The object or System.Object is the base Type for all other types in C#.
  • It can store any value and any other Type can be implicitly converted to it:
  • It is the best way to declare variables without a type though.
  • To use an object we need to cast it back to the required type each time.

Syntax

 object type_object_1 = “hello world”;
object type_object_2 = 55; // Value type 
object type_object_3 = true; // Value type
object type_object_4 = new MyCompany();  

Var:

  • The ‘var’ is used to declare an implicitly typed variable.
  • It is type-safe as the C# Compiler will determine the type based on what we initialize the variable to.
  • Var is only used for variables defined in a method body.
  • It is not used as a return type
  • Var cannot be set to null (i.e. var student= null; is not valid)
  • Var must be initialized (i.e. var student; only declaration is not valid either)
  • As with an explicitly declared variable, a ‘var’ cannot be re-assigned to another type.

Syntax:

 var type_implicit = “hello world”;   

Dynamic :

  • C# was 4.0 introduced a new type called "Dynamic".
  • It is a new static type that is not known until runtime.
  • At compile time, an element with it is assumed to support any operation so we need not be worried about the object, whether it gets the value from COM, the DOM, from reflection, or anything else in the program.
  • Before dynamic errors are caught at runtime only.
  • But, The dynamic type in C# uses the Dynamic Language Runtime (DLR).
  • Hence, The dynamic status is compiled into a variable-type object that is dynamic at compile time, not at run time.

 Dynamic Language Runtime (DLR) in C#

Syntax:

 dynamic type_dynamic = “hello world";  

Difference between Object, Var, and Dynamic type

Let's see Object vs. Var vs. Dynamic type variables:

Object
Var
Dynamic
The object was introduced with C# 1.0
Var was introduced with C# 3.0
The dynamic was introduced with C# 4.0
It can store any kind of value because the object is the base class of all types in the .NET framework.
It can store any type of value but It is mandatory to initialize var types at the time of declaration.
It can store any type of variable, similar to the old VB language variable.
The compiler has little information about the type.
It is type-safe i.e. Compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
It is not type-safe i.e. Compiler doesn't have any information about the type of variable.
The object type can be passed as a method argument and the method also can return the object type.
Var type cannot be passed as a method argument and the method cannot return object type. Var type work in the scope where it is defined.
Dynamic type can be passed as a method argument and method also can return dynamic type.
Need to cast object variable to original type to use it and perform desired operations.
No need to cast because the compiler has all the information to perform operations.
Casting is not required but you need to know the properties and methods related to stored type.
Cause the problem at run time if the stored value is not getting converted to an underlying data type.
Doesn't cause a problem because the compiler has all the information about the stored value.
Cause problem if the wrong properties or methods are accessed because all the information about stored value is getting resolved only at run time.
Useful when we don’t have more information about the data type.
Useful when we don’t know the actual type i.e. type is anonymous.
Useful when we need to code using reflection or dynamic languages or with the COM objects because we need to write less code.

Read More - C# Interview Questions For Freshers

What do you think?

I hope you will enjoy these three types while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Also, Consider our C# Programming Course for a better understanding of all C# concepts.

FAQs

The difference between var and dynamic keywords in C# is that var is Early Bounded (it is statically checked, or you can say it is checked at compilation time), While dynamic is Late Bounded (the methods, the properties, the type, everything will be checked at runtime only)

The dynamic type has been added to C# since version 4 as because of the need to improve interoperability with COM (Component Object Model) and other dynamic languages

The dynamic type is a static type, but an object of type dynamic bypasses static type checking. 

Take our Csharp skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this