Home »
C# Tutorial
Difference between object, var, and dynamic types in C#
In this tutorial, we will learn about C# object, var, and dynamic types. Difference between object, var, and dynamic types.
By IncludeHelp Last updated : April 06, 2023
object Type
It is a type which is base of all the types and one of the oldest features of the language. It means values of any types can be stored in object type variable. But type conversion (un-boxing) is required to get original type when the value of variable is retrieved in order to use it.
Example
Object o;
o = 10;
So it is an extra overhead of un-boxing. Thus, object type should only be preferred if we have no more information about the data type.
Compiler has title information of object type.
var Type
The var type was introduced in .net 3.5 framework. This also can have any type of values like object but it is must to initialize the value at time of declaration of the variable. The var keyword is best suited for linq queries in C#.
Example
var a = 10;
var b = "string value";
// object of product type
var c = new Product();
var d; //Error, as the value is not assigned
// at time of declaration of variable d
d = 10;
dynamic Type
The dynamic type was introduced in .net 4.0 framework. This also can be used to store any type of value like object and var but unlike object un-boxing is not required at time of using the variable.
Example
dynamic d1 = 10;
dynamic d2;
D2=new Product();
dynamic d3="string value";
Compiler has no information of dynamic type but value is evaluated at run time.