0

I have a simple question which I can not seem to understand.

Why is this valid c#:

if (node != null)
{
     string fullAddress = node.InnerText;
}

And this is not?

if (node != null)
     string fullAddress = node.InnerText;

Is this a bug in the compiler or is this intended?

clamchoda
  • 4,411
  • 2
  • 36
  • 74
  • What you expect second version to do? `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:30
  • @Alexei Levenkov I expected it to not have a syntax error! – clamchoda Jul 10 '13 at 16:39
  • I mean what result you suggest this code to produce (ignoring the fact it is invalid C# syntax) - i.e. how that `fullAddress` variable would be visible *after* `if` statement in case of false condition. – Alexei Levenkov Jul 10 '13 at 16:44
  • 1
    possible duplicate of [Can’t declare local variable inside conditional statement](http://stackoverflow.com/questions/2246898/cant-declare-local-variable-inside-conditional-statement) – Eric Lippert Jul 10 '13 at 17:43
  • Also a duplicate of http://stackoverflow.com/questions/8823427/why-this-compile-error – Eric Lippert Jul 11 '13 at 14:58
  • possible duplicate of [Variable declarations following if statements](http://stackoverflow.com/questions/2496589/variable-declarations-following-if-statements) – nawfal Nov 02 '13 at 05:47

5 Answers5

7

Because you're defining a local variable without an enclosing scope.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
5

Well msdn say this

Declaration statements are permitted in blocks, but are not permitted as embedded statements.

Anirudha
  • 32,393
  • 7
  • 68
  • 89
4

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.

Michael Bray
  • 14,998
  • 7
  • 42
  • 68
  • Thank you Michael. I was always just thrown off why it produces a syntax error, but "no sane programmer would ever do this" and the compiler knows it is actually the correct answer. I never actually thought about how useless the "invalid" syntax was. I upvote your answer because it helped me understand, but Alexei got the bonus points for Grammar from section B.2.5! – clamchoda Jul 10 '13 at 16:47
  • +1. This is the only answer that actually explains the reasoning behind the decision by the compiler team, rather than just stating what the specification says. – David Klempfner Mar 20 '20 at 02:46
2

This is expected behavior and makes sense if you remember that if takes one statement - so scope of variable declaration would end immediate after it is declared if such syntax is allowed.

Details covered in C# 5.0 specification sections 8.5 (thanks Rob Harvey for link) and grammar in section B.2.5:

Section 8.5:

A declaration-statement declares a local variable or constant. Declaration statements are permitted in blocks, but are not permitted as embedded statements.

Grammar from section B.2.5:

statement:
  labeled-statement
  declaration-statement
  embedded-statement

embedded-statement:
  block 
  ...

if-statement:
   if   (   boolean-expression   )   embedded-statement

As you can see variable declaration (declaration-statement) is not embedded-statement and hence can't be used in if-statement.

Note of C# specification location:

  • older version can be found online on MSDN (i.e. above mentioned Section 8.5 from Anirudh answer)
  • latest comes with VS installation and usually located in "Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Specifications\1033\CSharp Language Specification.docx" folder. See also Where can I find the C# 5 language specification?
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0
if (node != null)
  string fullAddress = node.InnerText;

This is Visual basic style of code writing where you do not have to use brackets. Also, in C# only one line of statement executes if we do not give the bracket. However, if we need to execute multiple more line of code then we will have to use bracket.

Keith Smiley
  • 61,481
  • 12
  • 97
  • 110
Manoj
  • 397
  • 2
  • 6