What is different between var, object and dynamic in C#.

var :

  1. it should be initialized
  2. We can not pass var in method as argument.
  3. Once the type is initialized it can not be changed. It become a strongly type.
  4. Var type is known at compile time. e.g var x = “strng1”; int len= x.Length, here compiler know that x is string type.

Object:

  1. It is not needed to initialize
  2. We can change and pass any type data in object.
  3. Type is resolved at run-time.

Dynamic:

  1. It is not needed to initialize.
  2. We can pass dynamic as argument in method.
  3. Has overhead on compiler for casting.
  4. Dynamic type is known at run time. e.g var x = “strng1”; int len= x.Length, here compiler uses reflection to know the type of x at run time and then check for length method in string.Dynamic internally uses reflection.

Leave a comment