Order of function modifiers in C#
I would like to know if there is a standard to set the order of functio开发者_如何学Cn modifiers in C#. i.e.
public static void Method()
{}
static public void Method()
{}
this both work well, BUT
when I code:
public void static Method()
{}
I receive the following error:
Member modifier 'static' must precede the member type and name
and
Method must have a return type
Method declarations must always follow this pattern:
[modifiers] returnType methodName([parameters])
There is no rule regarding the order of modifiers, but they must always precede the return type.
I don't think there is any standard order, people just do as they please... Personally I prefer to put the access modifier (public
, private
, etc) first, then the static
modifier (if any), then the virtual
, abstract
or override
modifier (if applicable).
See the C# spec for details (§10.6)
The problem is that void
isn't a modifier - it's the return type. All the modifiers have to come before the return type.
I'm pretty sure there is a convention for the ordering of genuine modifiers, but I don't know where it's documented.
I would always write the accessibility (public etc) first.
There is no specific order for method modifiers.
Following is the formal grammar from the C# Standard specification ...
Methods are declared using method-declarations:
method-declaration:
method-header method-body
method-header:
attributesopt method-modifiersopt partialopt return-type member-name type-parameter-listopt
( formal-parameter-listopt ) type-parameter-constraints-clausesopt
****method-modifiers:****
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
return-type:
type
void
member-name:
identifier
interface-type . identifier
精彩评论