ProfK
ProfK

Reputation: 51104

What's with using C# attributes with the 'Attribute' suffix?

I'm looking at some C# code that applies several LINQ to SQL attributes with the Attribute suffix, e.g. ColumnAttribute, instead of the plain Column that I am used to using. Is there any reason but verbosity to do this?

Upvotes: 7

Views: 2136

Answers (7)

Steve Morgan
Steve Morgan

Reputation: 13091

The attribute is called ColumnAttribute. The compiler just provides syntactic-sugar to allow you to specify them without the Attribute suffix.

There's no practical difference.

Upvotes: 3

Gustavo Mori
Gustavo Mori

Reputation: 8386

Column is simply the nickname of ColumnAttribute, and syntactically identical in functionality, and allowed by the compiler in either form. Some people prefer to use the full name, out of habit of following the Framework Naming Guidelines, which encourage adding Attribute to custom attribute classes, or I to interface types.

Upvotes: 1

Darin S.
Darin S.

Reputation: 1

If you look at the Philips Healthcare - C# Coding Standard, rule 3@122, you can see they actually want coders to add the suffix "Attribute" to attributes. http://www.tiobe.com/content/paperinfo/gemrcsharpcs.pdf

The code may be generated but the author(s) of the code generator are probably trying to create code that meets as many standards as possible.

Upvotes: 0

Nicole Calinoiu
Nicole Calinoiu

Reputation: 21002

No, it's just a style decision. However, if what you're looking at is code generated using CodeDOM the presence of the suffix is expected since C# code generator will keep the full ___Attribute type name when adding an attribute.

Upvotes: 2

Peter O.
Peter O.

Reputation: 32908

Most attributes end with the word Attribute, including ColumnAttribute, CLSCompliantAttribute, and SerializableAttribute. The compiler allows the last word Attribute to be omitted. It's the programmer's choice whether to add Attribute to such names or not.

The Attribute suffix, however, is merely a convention: it is perfectly valid, albeit unusual, to define an attribute, for example, as follows:

    [AttributeUsage(AttributeTargets.All)]
    public class Foo : Attribute {

    }

just as it is to define an exception named Throwable, for example.

Upvotes: 3

Kilanash
Kilanash

Reputation: 4569

They're syntactically the same. AFAIK there's no reason to do this unless the person in question wasn't familiar with the fact that syntax sugar in C# adds the 'Attribute' suffix automatically when adding an attribute via square brackets.

Upvotes: 1

Sven
Sven

Reputation: 22703

There is no semantic difference. Probably whoever wrote the code just preferred that notation.

It's also possible that the code was automatically generated using a tool. Code generation tools usually don't bother to strip the Attribute bit from the attribute's type name.

Upvotes: 7

Related Questions