tags:

views:

91

answers:

3

Hi,

I would like to know if there is a standard to set the order of function modifiers in C#. i.e.

public static void AssignTo(this int me, int value)
{}

static public void AssignTo(this int me, int value)
{}

this both work well, BUT

when I code:

public void static AssignTo(this int me, int value)
{}

I receive the following error:

Member modifier 'static' must precede the member type and name

and

Method must have a return type

Any help/link would be appreciated. TIA.

+4  A: 

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.

Jon Skeet
+5  A: 

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... Personnally 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)

Thomas Levesque
Good points Thomas, please any other self-created "modifiers pattern"?
Ramon Araujo
Thomas, could you please add a link to "See the C# spec for details (§10.6)". I have downloaded the specifications but haven't find them now.
Ramon Araujo
There is no online version of the spec, so I can't post a direct link... In version 3.0 of the spec, §10.6 is on page 295
Thomas Levesque
I was speaking about this: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=DFBF523C-F98C-4804-AFBD-459E846B268E Also, for C# 3.0 exists: http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/csharp%20language%20specification.doc
Ramon Araujo
+1  A: 

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:
attributes*opt* method-modifiers*opt* partial*opt* return-type member-name type-parameter-list*opt* ( formal-parameter-list*opt* ) type-parameter-constraints-clauses*opt*

*method-modifiers:*
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern
return-type:
type
void
member-name:
identifier
interface-type . identifier

mumtaz