28.3. Naming attribute classes and attribute specifications

By convention, attribute class names are post-pended with 'Attribute' (we have named our attribute classes AuthorAttribute, StatusOfClassAttribute, and BuggyAttribute). This is not compulsory, but it is conventional and highly recommended.

You might have realized that when using the three attributes as attribute specifications in MyClass.cs, I used the shortcut Author instead of AuthorAttribute, and Buggy instead of BuggyAttribute. The C# compiler automatically searches for the AuthorAttribute class when it encounters 'Author' in an attribute specification. [3]

[3] It is possible to write two separate attribute classes – one by the name of AuthorAttribute, and the other called Author. In such cases, ambiguity will arise when 'Author' is used in attribute specifications. There will be a compilation error in such cases. Nevertheless, you shouldn't be writing attribute classes with such confusing names. Stick to naming all your custom attribute classes SomethingAttribute.

You can replace the [Author] attribute tag in attribute specifications with [AuthorAttribute] with no consequences. C# gives you this flexibility.

This code:

6:   [Author("Mok","21 Dec 02")]
7:   public void DoSomething(){
8:     // some code
9:   }

and this:

6:   [AuthorAttribute("Mok","21 Dec 02")]
7:   public void DoSomething(){
8:     // some code
9:   }

are therefore equivalent.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.163.250