When you write an if
without braces, the compiler treats the single statement as if there were braces, so:
if (node != null)
string fullAddress = node.InnerText;
essentially gets turned into:
if (node != null)
{
string fullAddress = node.InnerText;
}
However, note that the scope of fullAddress
is only within the braces, so the variable can never be used. The compiler is smart enough to know this, and so it flags it as an error because it knows that no sane programmer would ever do this. :)
I think this is actually a common theme in the .NET compilers - they have a lot of sanity checking to make sure you don't do something that doesn't make sense, and will often optimize their output based on various code patterns.
fullAddress
will not be visible outside that single line if it is allowed... (Check C# specification - most likely declaration is not statement) – Alexei Levenkov Jul 10 '13 at 16:30fullAddress
variable would be visible afterif
statement in case of false condition. – Alexei Levenkov Jul 10 '13 at 16:44