blog

Home / DeveloperSection / Blogs / Difference between Object type and Var type

Difference between Object type and Var type

Vijay Shukla4240 04-Dec-2012
Object type:

       Object type is able to store any type of value, because object is the base class of all type. Object type Require to type cast object variable to original type before using it. So this assigning to object type and converting to original type called as Boxing and Un-Boxing for value type.

I’m giving example using simple C# Console base application because MVC have same syntax for variables.

Example:
using System;
 class A
{
   public static void Main(String []args)
   {
       Object x=10;
       Object y=20;
 Console.WriteLine(Convert.ToInt32(x)+Convert.ToInt32(y));
   }
}
Var type:

            Var type is able to store any type of value but it requires initializing at the time of declaration. Var type no needs to cast because compiler has all information to perform operation. It's define only locally(function level variable) not globally (class level variable). 

Example:
using System;
 class A
{
   public static void Main(String []args)
   {
       var x=10;
       var y=20;
   Console.WriteLine(x+y);
   }
}

Updated 18-Sep-2014

Leave Comment

Comments

Liked By