Bennett Yeo
Bennett Yeo

Reputation: 829

C# attribute formatting conventions

Lately I've seen attribute tags formatted into two ways in C# (even in the official microsoft guide):

[foo]
public class bar {...}

and

[foo] public datatype bar;

Is there any advantage to where the tag is placed? Should the tag be placed in a certain position based on whether it is over a class or a datatype?

Upvotes: 3

Views: 954

Answers (1)

bkr
bkr

Reputation: 1484

it's personal preference, it's not going to make any difference to the compiler, pick one and be consistent.

That said, I prefer the 1st format in both cases because it's possible to have multiple attributes on both classes and datatypes - and I find it easier to read spread out. it would get messy quickly if you did that in-line.

take the following sample code for a class for example:

[Author("P. Ackerman", version = 1.1)]
[Author("R. Koch", version = 1.2)]
class SampleClass
{
    // P. Ackerman's code goes here... 
    // R. Koch's code goes here...
}

putting it inline just makes it unreadable.

Upvotes: 3

Related Questions