3

I had thought that this was one of the few solved problems in C# coding standards / style; Attributes always appear on the line above the thing to which they are applied a la

[SomeClassAttribute]
class SomeClass
{
   [SomeFieldAttribute]
   bool someField;

   [SomeEnumAttribute]
   SomeEnum
   { 
     SomeValue
   }

   [SomeMethodAttribute]
   void SomeMethod() 
   { 
   }
}

But recently I've started to see Attributes in-line with the thing to which they are applied:

[SomeClassAttribute] class SomeClass
{
   [SomeFieldAttribute] bool someField;

   [SomeEnumAttribute] SomeEnum
   { 
     SomeValue
   }

   [SomeMethodAttribute] void SomeMethod() 
   { 
   }
}

For example on the Microsoft website and in the FileHelpers open source project. Is this just laziness on the part of the author of example code, or something that people actually use on a day to day basis, and if so why?

5
  • That's interesting: I've not previously come across such placement.
    – BoltClock
    Commented Apr 16, 2011 at 11:49
  • Purely stylistic. That being said, I can't imagine using anything but the first style...
    – Cody Gray
    Commented Apr 16, 2011 at 11:55
  • Me neither, when I saw it on the FileHelpers site I thought it was an aberration, but seeing it on the Microsoft site for a new .Net attribute made me wonder.
    – B Tyler
    Commented Apr 16, 2011 at 11:56
  • Note that there is one case where attributes are usually written inline: method parameter attributes, such as void Foo([In] string s).
    – svick
    Commented Apr 16, 2011 at 12:42
  • @svick Good point
    – B Tyler
    Commented Apr 16, 2011 at 13:09

3 Answers 3

6

You can't put the attribute on the line above if it's being applied to a parameter.

e.g. this example from an ASP.NET MVC book

public ViewResult List([DefaultValue(1)] int page)
{
    // ... 
}

Maybe this has led to some people choosing to write attributes inline?

Also another example involving LINQ to SQL mappings..

[Column] public string Name { get; set; }
[Column] public string Description { get; set; }
[Column] public decimal Price { get; set; }
[Column] public string Category { get; set; }

The [Column] attribute almost feels like another modifier like the public access or the type, I can see how it might feel comfortable to write it inline like that.

2
  • I think that you could put it above though for a method parameter, it's just that it might get a bit clunky.
    – B Tyler
    Commented Apr 16, 2011 at 13:11
  • @B Tyler - yep I suppose there's no reason to have all your parameters on one line of code, you could put them on separate lines with attributes above. Probably would violate some other code style rules, though! :-) Commented Apr 16, 2011 at 22:08
6

The two pieces of code are identical. It's merely a styling choice.

I've always preferred the former style, so I'll keep doing that. Use the convention that is easier for you and your team.

2

The first method is preferred - you may have several attributes, and some of them very long, for example:

 [DebuggerDisplay("Collection with {Count} elements of {User} user")]
 [Conditional("DEBUG")]
 [Serializable]
 ... some security attributes ...
 ... some application specific attributes ...
 ... some resharper attributes ...

I have no idea on how to write them in one line and maintain readability...

2
  • With a comma, perhaps?
    – Cody Gray
    Commented Apr 16, 2011 at 12:09
  • I didn't really mean that to sound like a question. The actual answer is to separate each attribute with a comma. This issue has already been solved.
    – Cody Gray
    Commented Apr 17, 2011 at 4:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.