Var i; how many values return?
This question confused me. please help me in exact way....
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Sep 14, 2014, 6:09 AM EDT
var i;
doesn't return any values at all. It's simply a variable declaration.
Moreover, it's not even valid C# syntax except in the very unusual circumstance that you have a class or struct called 'var' in scope within your application. In that case, it declares 'i' to be a variable of the 'var' type.
The much more usual use of 'var' is to declare a local variable's type implicitly. The compiler infers the type of the variable by working out the type of the expression on the right hand side of the '=' sign. For example:
var i = 3; // 'i' inferred to be of type int
var j = true; // 'j' inferred to be of type bool
var k = new MyType(); // 'k' inferred to be of type MyType
Clearly, some expression needs to be assigned to the variable for the compiler to be able to infer the type.
Notice that the variable is still 'strongly' typed i.e. you can't then assign an expression of a different type to it. For example this is illegal:
i = 2.5; // illegal, 'i' is of type int not double.
Jignesh TrivediRank #16 · 50.3K reputationPosted Sep 14, 2014, 8:11 PM EDT
Var keyword define type of variable implicit. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type
Please refer
http://msdn.microsoft.com/en-us/library/bb383973.aspx
http://www.dotnetperls.com/var
Agree with Vulpes
Var I; does not return anything but it gives compilation error....
hope this will help you.
Arunava BhattacharjeeRank #635 · 3.8K reputationPosted Sep 15, 2014, 1:52 AM EDT