Click here to Skip to main content
15,891,846 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How Var is different from Object. Object is a generic class, all the other classes say string, int32 etc are derived from it. but i cannot understand var, i've read number of articles. please clear my concept. thanks in adnvance.
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Dec-11 23:31pm    
Makes sense. Comparison with object is too stupid to invent a good explanation of why it's wrong, so this article is useful, my 5.
I've just chosen to answer to the point, please see.
--SA
Monjurul Habib 28-Dec-11 14:58pm    
5!
First, System.Object is not a generic class at all. It is a "normal" non-generic class, a base class of all types.

Now, any class is a type, but var is not a type at all. If is a keyword used for type inference. See:
http://en.wikipedia.org/wiki/Type_inference[^],
http://msdn.microsoft.com/en-us/library/ms364047%28v=vs.80%29.aspx[^].

Simplest example:

C#
var reader = System.Io.StreamReader(fileName);


When reader is declared like that, a compiler works with this name exactly as it was declared as System.Io.StreamReader. Why? Because a compiler can figure out that there is no other options. In this example, var works like "type-that-need-not-to-be-named", "type-you-know-which". A compiler can infer the name in much more difficult situations. For example, instead of constructor it could be a function, one of several functions with the same name:

C#
string RightSideOperand(bool isEmpty) { if (isEmpty) return string.Empty; else return "Sample string"; }
double RightSideOperand() { return 3.14159; }

var stringVariable = RightSideOperand(true); // stringVariable type is inferred as string
var doubleVariable = RightSideOperand(); // stringVariable type is inferred as double


There are even more complex cases, and many of them are not related to var.

[EDIT]

Now, some advice about correctness of your questions. The questions "What is the difference between {0} and {1}?" cannot be correct, and your question is a good illustration of such incorrectness. You can understand it if you see my answer.

I explained it in my past answer: what is the difference between the class and encapsulation in programming[^].

Please see the discussion about good and bad questions: How to ask a good question?[^].

Best wishes,
—SA
 
Share this answer
 
v2
Comments
Amir Mahfoozi 28-Dec-11 0:26am    
+5
Sergey Alexandrovich Kryukov 28-Dec-11 0:28am    
Thank you, Amir.
--SA
Monjurul Habib 28-Dec-11 14:58pm    
5!
Sergey Alexandrovich Kryukov 28-Dec-11 17:44pm    
Thank you, Monjurul.
--SA
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

C#
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
 
Share this answer
 
v2
Comments
LaxmikantYadav 27-Dec-11 23:26pm    
My 5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900