Difference between Object, Dynamic and Var

Object:

Each object in C# is derived from object type, either directly or indirectly. It is compile time variable and require boxing and unboxing for conversion and it makes it slow. You can change value type to reference type and vice versa.

public void CheckObject()
{
    object test = 10;
    test = test + 10;    // Compile time error
    test = "hello";      // No error, Boxing happens here
}

Var:

It is compile time variable and does not require boxing and unboxing. Since Var is a compile time feature, all type checking is done at compile time only. Once Var has been initialized, you can't change type stored in it.

public void CheckVar()
{
    var test = 10;         // after this line test has become of integer type
    test = test + 10;      // No error
    test = "hello";        // Compile time error as test is an integer type
}

Dynamic:

It is run time variable and not require boxing and unboxing. You can assign and value to dynamic and also can change value type stored in same. All errors on dynamic can be discovered at run time only. We can also say that dynamic is a run time object which can hold any type of data.

public void CheckDynamic()
{
    dynamic test = 10;
    test = test + 10;     // No error
    test = "hello";       // No error, neither compile time nor run time
}

Everything is Object because it is a base type for every type in .net environment. Every type inherit from Object in a moment, a simple int variable can be boxed to an object and unboxed as well. For example:

object a = 10; // int
object b = new Customer(); // customer object
object c = new Product(); // product object
object d = "Jon"; // string
object e = new { Name = "Felipe", Age = 20 }; // anonymous type

It is the most abstraction for any type and it is a reference type. If you want to get the real type, you need to unbox it (using a conversaion strategy such as methods, casts, etc):

object a = "Some Text";
string text = a.ToString();

// call a string method
text = text.ToUpper();

object i = 10; // declared as object but instance of int
int intValue = (int) i; //declare as an int ... typed

Dynamic is an implementation of a dynamic aspect in C#, it is not strongly typed. For example:

dynamic a = new Class();
a.Age = 18;
a.Name = "Jon";
a.Product = new Product();

string name  a.Name; // read a string
int age = a.Age; // read an int
string productName = a.Product.Name; // read a property
a.Product.MoveStock(-1); // call a method from Product property.

var is just a keyword of the C# language that allows you define any object of a type since you initialize it with a value and it will determinate the type from this value, for example:

var a = 10; // int
var b = 10d; // double
var c = "text"; // string
var d = 10m; // decimal
var p = new Product(); // Product type

The compiler will check the type of the value you have defined and set it on the object.


1) var is used for implicit type definition. For example if you define a variable like this:

var number = 123;

Compiler infers the type based on the assigned value and your variable initialized as integer in compile time. After this definition you can't assign a string to your variable because it is an integer.And you can't use var like this:

var number;
number = 123;

Because you have to assign something to your variable if you are using var keyword so that the type can be determined.

2) Object is a base class for all classes. In C# all classes inherits from object class, therefore you can assign everything to an object.For example:

object str = "bla bla bla..."; 
str = 234;
str = DateTime.Now;

This is working because when you doing this boxing/unboxing performing automatically for you. And as opposed to var keyword you can use object like this:

object myVariable;
myVariable = "bla bla bla..";

3) dynamic is a cool feature that came with C# 4.0, you can use dynamic if you don't know returning type from your function in compile time.Your type will be determined in run-time.Therefore you can't use intellisense with dynamic variables.

You can use dynamic like this:

dynamic myObj = SomeMethod();
myObj.DoSomething();
myObj.Method1();

But you must be careful when you are using dynamic.Because if you call a method or property which doesn't exist you will get a RuntimeBinderException in runtime.

And last thing I want to mention, dynamic and object can be parameter type, but var can't. For example you can do this:

public void SomeMethod(dynamic arg1)

But you can't do this:

public void SomeMethod(var arg1)

Because var is not a type rather it's a syntactic sugar to let the compiler infer the type for you.