[英]What is max number of valid modifiers you can have while declaring a class in c#?
This question is more for curiosity.这个问题更多是出于好奇。
By modifiers I mean, keywords that you add before declaring a class.我所说的修饰符是指在声明类之前添加的关键字。
eg例如
//1 2 3
public abstract partial class Foo
{
}
This have 3 modifiers public
, abstract
, and partial
.这有 3 个修饰符
public
、 abstract
和partial
。
Zero modifiers, in this question perspective, would be something like从这个问题的角度来看,零修饰符类似于
class Foo
{
}
What is the max number of valid modifiers you can have before the name of the class when declaring it?声明类名之前可以拥有的最大有效修饰符数是多少?
The maximum number is...最大数量是...
(or 6 if you count unsafe
) (或 6 如果你算
unsafe
)
According to here , a class declaration looks like this:根据here ,类声明如下所示:
class_declaration
: attributes? class_modifier* 'partial'? 'class' identifier type_parameter_list?
class_base? type_parameter_constraints_clause* class_body ';'?
;
You probably don't count the attributes as "modifiers", but you probably would count the partial
keyword as a "class modifier", though it is not an actual class_modifier
.您可能不会将属性视为“修饰符”,但您可能会将
partial
关键字视为“类修饰符”,尽管它不是实际的class_modifier
。
The list of class_modifier
is: class_modifier
的列表是:
class_modifier
: 'new'
| 'public'
| 'protected'
| 'internal'
| 'private'
| 'abstract'
| 'sealed'
| 'static'
| class_modifier_unsafe
;
We know the longest access modifier is protected internal
, so we can ignore all the access modifiers:我们知道最长的访问修饰符是
protected internal
,因此我们可以忽略所有访问修饰符:
class_modifier
: 'new'
| 'abstract'
| 'sealed'
| 'static'
| class_modifier_unsafe
;
You can only choose one of sealed
, static
and abstract
, so we are left with 6 "keywords":您只能选择
sealed
、 static
和abstract
,因此我们剩下 6 个“关键字”:
protected
internal
new
sealed
/ static
/ abstract
sealed
/ static
/ abstract
partial
unsafe
Note that new
and protected internal
can only be used in nested class declarations.请注意,
new
和protected internal
只能在嵌套类声明中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.