Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
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 –  Eric Lippert Jul 10 '13 at 17:43
    
Also a duplicate of stackoverflow.com/questions/8823427/why-this-compile-error –  Eric Lippert Jul 11 '13 at 14:58

5 Answers 5

up vote 2 down vote accepted

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?
share|improve this answer

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

share|improve this answer
    
+1 That's a lot more succinct than how I was going to explain it. –  lc. Jul 10 '13 at 16:30
    
Thanks. Wish I knew enough about the C# specification to quote that part of the specification that states this rule, but I suspect that it is implied, rather than stated. –  Robert Harvey Jul 10 '13 at 16:31
    
@RobertHarvey it is more or less explicit in the specification - I've added excerpt in my answer. –  Alexei Levenkov Jul 10 '13 at 16:36
    
@AlexeiLevenkov: It's specifically forbidden in Section 8.5 of the specification. –  Robert Harvey Jul 10 '13 at 16:37

Well msdn say this

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

share|improve this answer
    
Ah, there you go. –  Robert Harvey Jul 10 '13 at 16:33

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.

share|improve this answer
    
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
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.