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.
它确实编译了,但它显示它是冗余的。
But when I did that with fields (also by accident) , I got an error (compilation) :
但是,当我用字段(也意外地)做这些时,我得到了一个错误(编译):
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.
Nb我已经知道不允许var和字段的其他限制。但这里有一些不同的东西。
5 个解决方案
#1
82
;
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.
;单独是一个语句(空语句),但只允许在类的主体中声明声明语句;其他类型的语句只能出现在方法的主体中。
#2
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
;它本身就是一个空的语句。在类范围内,只允许声明语句。类主体是在c#规范5.0中定义的。
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.
因此只能声明字段和其他成员,但不能在类主体中使用其他语句。
#3
6
It is not part of local variable declaration, it's a statement by itself, as indicated by Thomas.
它不是局部变量声明的一部分,它本身就是一个声明,正如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
TLDR;这与变量/字段声明无关。
You might want to take a look at this thread too: When do you use scope without a statement in C#?
您可能还想看一下这个线程:在c#中,什么时候使用范围而没有声明?
It is kind of similiar, but not completely, it will help you to understand why
这是一种相似的,但不是完全的,它将帮助你理解为什么。
int a = 1;;;
int = 1;;;
is valid.
是有效的。
#4
3
In the first case the compiler sees a no-op statement. It doesn't matter that the second ;
comes after a variable declaration.
在第一个例子中,编译器看到一个无op的语句。第二个问题无关紧要;在变量声明之后。
In the second case the compiler sees an attempt to create an empty declaration which isn't allowed.
在第二种情况下,编译器看到试图创建一个不允许的空声明。
#5
-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.
在一个函数的内部是冗余的;是空语句,但在类声明中是未声明的字段,不允许。
#1
82
;
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.
;单独是一个语句(空语句),但只允许在类的主体中声明声明语句;其他类型的语句只能出现在方法的主体中。
#2
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
;它本身就是一个空的语句。在类范围内,只允许声明语句。类主体是在c#规范5.0中定义的。
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.
因此只能声明字段和其他成员,但不能在类主体中使用其他语句。
#3
6
It is not part of local variable declaration, it's a statement by itself, as indicated by Thomas.
它不是局部变量声明的一部分,它本身就是一个声明,正如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
TLDR;这与变量/字段声明无关。
You might want to take a look at this thread too: When do you use scope without a statement in C#?
您可能还想看一下这个线程:在c#中,什么时候使用范围而没有声明?
It is kind of similiar, but not completely, it will help you to understand why
这是一种相似的,但不是完全的,它将帮助你理解为什么。
int a = 1;;;
int = 1;;;
is valid.
是有效的。
#4
3
In the first case the compiler sees a no-op statement. It doesn't matter that the second ;
comes after a variable declaration.
在第一个例子中,编译器看到一个无op的语句。第二个问题无关紧要;在变量声明之后。
In the second case the compiler sees an attempt to create an empty declaration which isn't allowed.
在第二种情况下,编译器看到试图创建一个不允许的空声明。
#5
-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.
在一个函数的内部是冗余的;是空语句,但在类声明中是未声明的字段,不允许。