Click here to Skip to main content
15,896,369 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have faced one question from CTS ie

suppose

int i=10;
int j=20;

then var s= i.z;

what happens if we can do like this and what is the output of that?

their main concern is what is the var key word does?
Posted
Comments
Anoop Kr Sharma 28-Sep-13 15:01pm    
[no name] 28-Sep-13 16:09pm    
It won't do anything. Your integer (i) does not have a property named "z" so your code will not even compile.
Sergey Alexandrovich Kryukov 29-Sep-13 1:49am    
What's wrong with just reading language documentation?
—SA

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Sep-13 1:51am    
Exactly, a 5. Why asking such question of documentation is clear enough?
—SA
It's for when a type isn't known until run compile time -- as with Linq's anonymous types.
 
Share this answer
 
v2
Comments
Andreas Gieriet 28-Sep-13 16:20pm    
No, that's wrong. var is known at compile time. You maybe confuse this with dynamic.
var tells the compiler to deduce the actual type at compile time. It allows to have anonymous types passed around. Anonymous types are know at runtime but have no name to access them. They are produced by new { X, Y }; where X and Y are known entities.
Cheers
Andi
PIEBALDconsult 28-Sep-13 19:11pm    
Thank you. I don't use Linq, so I'm less familiar with the syntax.
Sergey Alexandrovich Kryukov 29-Sep-13 1:50am    
Of course you are right, Andreas. Incorrect answer.
—SA

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