Difference Between Var, Dynamic And Object type In C#

In this article, I am going to explain var, dynamic type, and object in detail. I will explain all types using examples. Var keyword was introduced in C# 3.0, while dynamic type was introduced in C# 4.0. Because this is a question that most interviewers will ask, you should be familiar with these three keywords before attending .net interview.

Let's start with "var" keywords first.

  • Var keywords are introduced in C# 3.0.
  • You need to assign a value for the var keyword while declaring variables.
  • Because its type is determined at compile time, it is a statically typed variable.
  • You cannot change the data type of a variable once it has been declared using the var keywords.
  • It provides intelligence once a value is assigned.

Now I will take an example of the var keyword in the c# console application, which will help us understand the above points step by step.

  • Declaration of the var keyword is not allowed without initialization because it checks the type at compile time. Please check the below snippet, which gives an error for initialization.

Difference between Var, Dynamic and Object keyword in c#

  • At compile time, determine the datatype. As shown in the below snippet, the salary variable is assigned an int type, so IntelliSense will show the salary variable as an int datatype.

Difference between Var, Dynamic and Object keyword in c#

  • You cannot change the data type of the Var variable once it has been assigned a value. In the below snippet, we can change the datatype of the salary variable. If we try to convert from int to string data, the compiler will complain that it "cannot implicitly convert string to int." 

Difference between Var, Dynamic and Object keyword in c#

  • IntelliSense shows the var keyword as it decides its datatype at compile time. As we can see, IntelliSense is enabled in the below snippet for the int type.

Difference between Var, Dynamic and Object keyword in c#

Let's discuss the "dynamic" keyword.

  1. It was introduced in C# 4.0.
  2. It can store any type of value, and the type of the variable is unknown until runtime, so it will not support IntelliSense.
  3. It's not mandatory to initialise at declaration time.
  4. Dynamic types can be passed method parameters.

The developer will not be able to identify the type of the variable at compile time, so it doesn't support IntelliSense either. Please refer to the code snippet

Difference between Var, Dynamic and Object keyword in c#

Declaration of a dynamic type is allowed without initialization because its type is determined at runtime. Please see the following snippet, which displays an IntelliSense not available for dynamic type.

Difference between Var, Dynamic and Object keyword in c#

Dynamic types can be passed method parameters.

static void Main(string[] args) {
    dynamic salary;
    salary = 10;
    Console.WriteLine(salary);
    GetData(salary);
}
private static void GetData(dynamic salary) {
    Type type = salary.GetType();
    string typeName = type.Name;
    if (typeName == "Int32") {
        int salaryInInt = Convert.ToInt32(salary);
    } else {
        Console.WriteLine("It is not an Integer");
    }
}

Let's discuss the "object" keyword.

  1. The object was introduced in C# 1.
  2. It can store any type of value because it is the base class for all types in C#.
  3. You need to typecast an object value to a specific type before you perform any manipulation on it.
  4. You need to be very careful while using objects because it will create problems at runtime if it is not able to convert to a specific type while being typecast.

The object can store any type of value. Please see the code snippet below. 

static void Main(string[] args) {
    object x = 10;
    object y = "C# corner";
    object z = 100.10;
    object total = Convert.ToDecimal(x) + Convert.ToDecimal(z);
}

You need to typecast an object value to a specific type before you perform any manipulation on it. Check out the example below to see what I mean. 

private static void GetData(object salary) {
    Type type = salary.GetType(); //Here GetType() to get type of of object
    string typeName = type.Name;
    if (typeName == "Int32") // Check for specific type, Here checked for int type
    {
        // This code will execute only if parameter value is type of integer else will return this values is not an integer
        int addPercentageToSalary = Convert.ToInt32(salary) + (Convert.ToInt32(salary) * 10 / 100);
    } else {
        Console.WriteLine("It is not an Integer");
    }
}

Now let's check the difference among all three

  1. Introduced in which version?
    • Object type were introduced in C# 1.0.
    • Var keyword was introduced in C# 3.0.
    • Dynamic type introduced in C# 4.0.
  2. declaration and initialization
    • Object: Initialization is not required at the time of declaration.
    • Var: Initialization is mandatory.
    • Dynamic: Initialization is not mandatory.
  3. Value storage 
    • Object: Because object is the base class for all types, it can store any type of value.
    • Var: It can store any type of value, but initialization is mandatory, so once you initialise with one type, the type cannot change.
    • dynamic: can store any type of value; we can also change the type in the following use.
  4. Is casting required?
    • Object: Before performing any operation on an object, it must be cast to its original type.
    • Var: casting does not require it because we have type information at compile time and intellisense, which will assist us in determining the type of variable.
    • Dynamic: need to cast to orignal type before performing any operation on it.
  5. can be passed as a method argument?
    • Object: Yes, we can pass it as a method argument.
    • Var: No.
    • dynamic, just like an object, we can pass dynamic as a method parameter.
  6. Will it support Intellisense?
    • Object: No
    • Var: Yes, It supports intellisense because it is known at compile time.
    • Dynamic: No, compilers do not have information at compile time.

Summary

In this article, we learned about three types: var, dynamic, and object. As explained in the article, each of the three has its own set of applications. I hope this will help you.

Thanks for reading.


Similar Articles