I stumbled across this while starting to learn about vars here:
http://msdn.microsoft.com/en-us/library/bb384061.aspx
However, I have no idea how that is a legal expression (it is). I thought you couldn't assign to something using an a = (b=c) because (b=c) does not produce a value to assign?
Thanks for any clarification on the matter.
a<<b for integers means "shift left". The bitwise representation of a is shifted left b bits. This is the same as multiplying by (2 to the power of b ).
For example, in the statement "v=i++", where the operator is in the postfix form, the value of "i" is assigned to "v" before the increment operation. In the statement "v =++i", where the operator is in the prefix form, the value of "i" is incremented first before being assigned to "v".
When int i is declared outside the for loop, it can be accessed outside the for loop and inside but when you define it inside the loop, it can only be used inside the loop. Usualy, if you need it for the for loop define it inside the loop.
It is legal. From the = operator C# reference page:
The assignment operator (=) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result.
(emphasis mine)
The code in the example is contrived (and pointless), but it can be used in other cases to do useful things in a more concise way. For example:
BinaryTree tree; TreeNode node; if((node = tree.FindNodeForKey(10)) != null) { // do useful things with returned node }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With