views:

143

answers:

6

What advantages do attributes have over the use of comments (or xml comments at the signature level)? I do remember seeing a thread in the C# section of this site somewhere, but I can't find it now.

Also, is it possible to apply an attribute to a specific parameter in a method (this was an interview question I read somewhere)?

+1  A: 

Attributes can be accessed at runtime, which is not the case of comments.

In fact, attributes and comments are two totally different things, and serve two totally different purposes.

Comments aim at providing hints to the programmer reading your code, while attributes aim at providing hints to the compiler/runtime "reading" your code ..

Brann
+2  A: 

I don't understand how attributes and comments are related; attributes add certain functionality to a class/object, while comments are meant to be in-line documentation.

It is possible to ease the addition of metadata using attributes (e.g., use an Author(string name) attribute to denote the author of a class, etc.), and thus easing the extraction of that metadata, but otherwise I don't see the comparison.

Jon Limjap
Sometimes attributes are used to add information to an object, but no additional functionality. Like the Author(string name) example you give. That is the only comparison I am thinking of.
dotnetdev
+1  A: 

Generally attributes are used when you plan to process the source code programmatically somehow (usually with .NET reflection.) To do the same with XML comments, you would have to have a special convention for the format of your comments and parse the source file yourself, and that convention couldn't be checked easily at compile-time like attributes are.

mquander
+4  A: 

Comments are just for the benefit of developers, so that they can see what classes and methods are about.

Attributes are useful to the solution you are developing as you can use then to describe how to work with the class you are adding attributes to.

Yes you can use attributes against parameters, but in my experience this makes for some complicated code, consider redesigning so your params can be grouped into a struct or class.

Here's a link about attributes:

http://en.csharp-online.net/Attributes

Hope this helps

Mark Dickinson
+1  A: 

Custom attributes are meant to be accessed at runtime using reflection. They can be applied to method parameters as well as generic type parameters, method return types, assemblies, type declarations, type members, and modules.

Mark Cidade
+1  A: 

Generally speaking, you cannot compare attributes an XML comments. The latter ultimately have absolutely nothing to do with the code, whereas attributes remain in the compiled assembly in metadata section.

As for the second part of your question, this is possible indeed:

[return:MySecondAttribute] void Foo([MyAttribute] int bar)

Here a MySecondAttribute is applied to the return value, and MyAttribute is applied to parameter bar. Replace return: with assembly: to apply an attribute at assembly level.

Anton Gogolev