To know which data type to use in different situations is generally a basic level decision.
The var is a nice one because you do not need to decide whether to use a System.String or System.Int32 becuase when you associate a value to the var, the compiler will determine the type for you.
IMP
Quote:
[using var does not remove the responsibility from understanding the memory usage, storage usage, percision, etc..of the data type. It's just makes things a bit eaiser and faster, but use it with caution.]
With var you will get access to the the data type class properties and members The same is not true for an object. It is not known at compile time what data type the value stored within the object is. You can store classes or generic lists in an object. I.e. both reference and value types can be stored in an object.
Let say you have a simple class named Person
public class Person
{
public string Name { get; set; }
public string Age { get; set; }
}
If we use var to contain the class, then the properties within the class are available, however if we use object they are not.
When to use an object in C#? Use object variables in situations when we will receive a list of data but you are not certain of the data types within them. For example:
List<object> fields = new List<object>(BuildFieldList(Query));
The Query identified above is a string of columns a user has selected from a reporting tool. We are not certain which column or how many the user has selected. Therefore we decide to use an object variable.
When to use a var in C#? Following along the same line as the above example, we would use a var to contain the result set from the Query that was executed.
I hope this is helpful