Why can I use the shorthand `[Authorize]` instead of the name of the actual class, `[AuthorizeAttribute]`?

I am browsing through the ASP.NET web stack source code and noticed that the AuthorizateAttribute class is actually named as such (see here).

So, why is it that I can use [Authorize] to apply the attribute...but that's not really the name of the Class. The name of the class is AuthorizateAttribute.

I actually copied/pasted the code into my own solution and renamed the class to JeffthorizeAttribute, and lo and behold, I can not add the [Jeffthorize] attribute to my MVC controllers.

Why does it accept the shortened version instead of the full class name? Is there something within the parent Attribute Class that automatically figures it out?

Just trying to understand :(

Jon Skeet
people
quotationmark

The C# compiler automatically tries appending the suffix Attribute when you're using an attribute (which is obvious syntactically). It just makes things easier. From the C# 5 spec section 17.2 (Attribute Specification):

By convention, attribute classes are named with a suffix of Attribute. An attribute-name of the form type-name may either include or omit this suffix. If an attribute class is found both with and without this suffix, an ambiguity is present, and a compile-time error results. If the attribute-name is spelled such that its right-most identifier is a verbatim identifier (ยง2.4.2), then only an attribute without a suffix is matched, thus enabling such an ambiguity to be resolved.

You should be able to use your [Jeffthroize] example - it's hard to tell why you can't in your specific situation, without seeing the code and the error message.

For example, this is fine:

using System;

[AttributeUsage(AttributeTargets.All)]
class FooAttribute : Attribute {}        

[FooAttribute] // Full name
class Test 
{
    [Foo] // Abbreviated name
    public static void Main() {}
}

If you tried to use [@Foo], that would be invalid - but [@FooAttribute] is valid.

Also note that VB has the same shorthand.

people

See more on this question at Stackoverflow