Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 729
ProfK Avatar asked Jul 15 '11 16:07

ProfK


2 Answers

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.

like image 175
Sven Avatar answered Sep 22 '22 13:09

Sven


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.

like image 20
Peter O. Avatar answered Sep 25 '22 13:09

Peter O.