Very quick question – whilst the following is possible in C#:
var a = new []{"hello"};
string[] b;
if ((b=a)!=null) { ... }
the following is not:
var a = new []{"hello"};
if ((string[] b=a)!=null) { ... }
Just wanted to confirm that I wasnt doing anything wrong in the second example and something along those lines are not possible. (In the similiar way aspects from the second example might be posible inside a using().
This is not possible, since in c# variable declaration is a ‘statement’, not an expression. Statements do not yield a value, therefore I do not think you can use it in a expression.
But the assignment, which you use in the
secondfirst form is an ‘expression’ which yield a result (value of a in this case), which can be used in another expression with an operator.More context from MSDN.
It specifically mentions:
So it looks more like a ‘language restriction’ more than ‘lack of a return value’, although that (lack of return values of statements) must have influenced such a restriction.