C
C#2mo ago
Foxtrek_64

Attribute Style Preference

Not sure if #help is the right place for this but I was hoping for some thoughts on attribute style.
// Style 1
[Foo]
[Bar]
public void Bizz(Buzz buzz) { }

// Style 2
[Foo, Bar]
public void Bizz(Buzz buzz) { }
// Style 1
[Foo]
[Bar]
public void Bizz(Buzz buzz) { }

// Style 2
[Foo, Bar]
public void Bizz(Buzz buzz) { }
It goes without saying that functionally, the two styles make absolutely no difference. They are handled identically by the compiler. I have noticed however that decompiled C#, such as when looking at lowered code in SharpLab, tends to prefer style 1. I think that's mostly because it's easier to render it as C# that way, not because of any functional reason. Which style do you guys prefer, and what is your reasoning? Does it depend, e.g. based on the length of the attribute name or the number of attributes? Do you mix and match styles?
3 Replies
Angius
Angius2mo ago
1 Different things are different things Same reason I do
var a = 1;
var b = 1;
var a = 1;
var b = 1;
and not
int a = 1, b = 1;
int a = 1, b = 1;
Foxtrek_64
Foxtrek_642mo ago
I personally use style 1 as well But yeah, your reasoning is sound. That was pretty much my thought for keeping them separate I could see it in some cases like [DisplayName("foo"), Alias("bar")] since they're dealing with similar topics, but I would personally prefer the library change to allow something like [DisplayName(name: "foo", alias: "bar")]
Aptivi
Aptivi5w ago
Style 1. With no exceptions, regardless of the length. This is so that I can easily tell how many attributes I've used. Some of my unit tests contain over 20 attributes, and if I am to do it in Style 2, it will be difficult to re-read them later.