Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ;; allowed after a local variable declaration, but not after a field declaration?

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.

like image 206
Royi Namir Avatar asked Dec 22 '14 11:12

Royi Namir


1 Answers

; 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.

like image 158
Thomas Levesque Avatar answered Oct 15 '22 08:10

Thomas Levesque