0

I see some code snippets like this:

[Description("This method is used to do something.")]
static void SomeMethod()
{
}

I am wondering if what we want is just to describe the meaning of the method, why not just use the following comments:

/// <summary>
/// This method is used to do something.
/// </summary>
static void SomeMethod()
{
}

Actually, the comment style can be leveraged by IntelliSense. So why do we bother to use an attribute?

Update

So, though not very accurate, I take Attributes as run time version of comment. While comment is only for edit time.

leppie
  • 115,091
  • 17
  • 196
  • 297
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • I've used it as a method to map enum across to user friendly text before via reflection see http://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value – Paul Sullivan Apr 24 '14 at 14:50
  • 2
    Yeah I think the most interresting point is that it can be accessed programatically even after compilation, while "comment style" are treated as comments so nowhere to be seen on compiled project. – Laurent S. Apr 24 '14 at 14:51
  • It powers the Properties window, you see the description text at the bottom of the window when you select the property or event. Not for a method of course, no idea why you'd use it. – Hans Passant Apr 24 '14 at 15:25

2 Answers2

1

Also it's a good mechanism to provide supporting information about the property if you are implementing a user control and want your property to show extra description in Properties Window. You can combine this attribute with CategoryAttribute to group Properties into categories in Properties Window

http://msdn.microsoft.com/en-us/library/system.componentmodel.descriptionattribute%28v=vs.110%29.aspx

Andrew
  • 3,648
  • 1
  • 15
  • 29
  • So this is actually another example of attribute providing runtime info while comments cannot. – smwikipedia Apr 24 '14 at 15:03
  • Well, this is the example of built-in functionality within Visual Studio. If you want your property to provide extra information for the user using your control, supply your property with `DescriptionAttribute` attribute. The fact that attributes are available at runtime applies to all attributes, and you can get those using reflection for later manipulation with types and their members. – Andrew Apr 24 '14 at 15:05
0

Attributes like [Description] can be accessed at runtime, while comments cannot. For example, this answer shows you how to get the description from an enum (and other answers there show how to parse an enum from its description). E.g. such an enum might look like:

public enum Fruit
{
    [Description("Apples are red or green, and tasty")]
    Apple,
    [Description("Pineapples are yellow inside, and acidic")]
    Pineapple,
}

Here's how it might look with your method's description:

var desc = typeof(ThatClass)
   .GetMethod("SomeMethod", BindingFlags.Static | BindingFlags.NonPublic)
   .GetCustomAttributes(typeof(DescriptionAttribute))
   .Cast<DescriptionAttribute>().ToList();
Console.WriteLine(desc[0].Description);
// prints "This method is used to do something."

I don't see much of a point to having it on a method, except in some unusual cases, but it has its uses and is quite distinct from a comment.

Community
  • 1
  • 1
Tim S.
  • 55,448
  • 7
  • 96
  • 122