59

I saw this weird behaviour and I wonder if there's a reasonable explanation for this:

When I put by ( by accident) an additional/extra semicolon in a function's local variable like:

public void MyMethod ()
{
    int a = 1;;
    Console.WriteLine(a); //dummy 
}

It does compile but it shows that it's redundant.

enter image description here

But when I did that with fields (also by accident) , I got an error (compilation) :

enter image description here

Question

Is there any reason for this restrictiveness in fields ?

Nb I already know the other restrictiveness thing for not allowing var with fields. But here it's something different.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 34
    How do you produce these beautiful screen-dump-patches? – DrKoch Dec 22 '14 at 11:30
  • 15
    C# support empty statements so ; on its own is valid within a method body, but you can't have statements in the body of a class so ; is not valid there. – Ben Robinson Dec 22 '14 at 11:32
  • 2
    @DrKoch [here](http://www.faststone.org/FSCaptureDetail.htm) – Royi Namir Dec 22 '14 at 11:33
  • 13
    "beautiful"? When did straight arrows become more beautiful than good old freehand circles? At least they are red. +1 for drop shadow. – kapex Dec 22 '14 at 19:26
  • @kapep Circles accuracy ( especially with code) can hide other stuff where an arrow is 100% precise without disturbing . also an arrow can provide "source" to "destination" ( just like my "Exception" to `;` arrow). there are places where a circle fits. this is not one of them :-) ( my opinion) - Example here http://i.imgur.com/UpklA5G.png , it hurts my eyes and prevents from reading it as a whole fluent line. – Royi Namir Dec 22 '14 at 19:29
  • 3
    @RoyiNamir This "source" to "destination" thing is a compelling argument. You could add a circle around the arrow to improve it even more! But anyway, it's more about the [free hand](http://meta.stackexchange.com/a/19775/168146) part than about the circle: http://i.stack.imgur.com/n5Y9I.png see the beauty? – kapex Dec 22 '14 at 20:00
  • @kapep Sorry I can't see the beauty. it looks like a 4 years old draw a circle. besides — the "beautiful" word is regarding the whole package : torn edges , shadows etc. _not_ just circle or arrow. but we can't have everybody happy. can't we ? :-) not to mention that you took my print screen and added the circle. so you really should compare myne to yours which is http://i.imgur.com/HBm7jHq.png . and here - there's no doubt. – Royi Namir Dec 23 '14 at 08:18

5 Answers5

83

; alone is a statement (empty statement), but only declaration statements are allowed in the body of a class; other kinds of statement can only appear in the body of a method.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
26

; itself is an empty statement. And in class scope only the declaration statements are allowed.The class body is defined in C# Specification 5.0, §10.1.6 Class Body

class-body:
{   class-member-declarations   }

For example you can't initialize a field in a separate statement:

class Foo 
{
    int x = 2; // this is allowed 
    x = 5; // this is not
}

So you can only declare fields and other members but you can't use other statements in a class body.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
6

It is not part of local variable declaration, it's a statement by itself, as indicated by Thomas.

This is valid:

public void MyMethod ()
{
    ;;;
    int a = 1;


    ;
    Console.WriteLine(a); //dummy 
    ;;
}

The idea of semi-colon statement is to allow such constructs:

while(mycondition) ;

It does not make sense to allow it in in the body of class, it brings no extra value.

TLDR; this has nothing to do with variable/field declaration

You might want to take a look at this thread too: When do you use scope without a statement in C#?

It is kind of similiar, but not completely, it will help you to understand why

int a = 1;;;

is valid.

Community
  • 1
  • 1
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
3

In the first case the compiler sees a no-op statement. It doesn't matter that the second ; comes after a variable declaration.

In the second case the compiler sees an attempt to create an empty declaration which isn't allowed.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
-4

Inside the body of a function the redundant ; is an empty statement but in the class declaration is an undeclared field and it not allowed.

  • 2
    Why would you post a new answer to a question that's already been answered, especially when your answer is wrong and less informative than the others? – siride Dec 25 '14 at 22:40