Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I mean "var" keyword.For example
C#
var a=10;
or
C#
int a=10;
.In my opinion program will work worth,becouse in situation of using var keyword,compiler should choose right type for given variable.
Posted
Comments
DamithSL 23-Jan-15 11:57am    
there is no run time performance difference when using var and not using var, can you elaborate more on your question?
Sergey Alexandrovich Kryukov 23-Jan-15 12:12pm    
Not only it does not affect performance; it has nothing to do with runtime at all; this is merely syntactic sugar. Please see the comment below and my answer.
—SA
Zoltán Zörgő 23-Jan-15 11:59am    
DamithSL is right: var is resolved at compile-time. Even before, as even Visual Studio can figure out what is the type.

1 solution

This is not an anonymous type; this is just a type obtained from type inference. Consider
C#
var stringBuilder = new System.Text.StringBuilder();
string whatIsIt = stringBuilder.GetType().FullName;
The last line will return "System.Text.StringBuilder", quite fully named :-); and the first line is strictly equivalent to
C#
System.Text.StringBuilder(); stringBuilder = new System.Text.StringBuilder();


So, this is nothing but syntax, syntactic sugar (http://en.wikipedia.org/wiki/Syntactic_sugar[^]).

You can try the same with the number and the string, will see a concrete named type.

I guess it's apparent when you can use this feature: when you know that "compiler's guess" is apparent and you get what you expected.

And anonymous type is something totally different: https://msdn.microsoft.com/en-us/library/bb397696.aspx[^].

—SA
 
Share this answer
 
v4

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