简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM