C # Anonymous region function
var foo = "bar";
new Func<String>(() =>
{
var foo = ""; // This can't be done in C#. Why is that?
/* In JavaScript, this is perfectly valid, since this scope (the anonymous
function) is disconnected from the outer scope, and any variable declared
within this scope will not affect variables in the outer scope */
})()
Actually, even in javascript it is not completely disabled; javascript allows lexical closures - so without the varold value should be available foo.
The difference is that javascript chooses so that you can re-declare the name with a different value (in the inner scope). C # chooses not.
I find the C # version less confusing! In particular, when the code (further in the method) expects to talk about the "old" variable, and suddenly it starts looking at the "new" one.