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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Arunava BhattacharjeePosted Sep 15, 2014, 5:52 AM
Jignesh TrivediPosted Sep 15, 2014, 12:11 AM
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.
VulpesPosted Sep 14, 2014, 10:09 AM
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.