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

Difference between Object type and Var type


ASP.NET MVC ASP.NET MVC 
Ratings:
0 Comment(s)
 2152  View(s)
Rate this:

Difference between Object type and Var type

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);

   }

}


Don't want to miss updates? Please click the below button!

Follow MindStick