Toby
Toby

Reputation: 10144

Difference between <SomeName()> and <SomeNameAttribute()> in VB.NET

Pretty much as the title, is there a difference between using and attribute <SomeName()> and <SomeNameAttribute()> where SomeName is the same in both cases?

for example in Newtonsoft's Json.NET there is <JsonConverterAttribute()> and <JsonConverter()> that can be applied to a member. According to the Object Browser both resolve to the same JsonConverterAttribute class. Is that a peculiarity of this particular attribute or framework, or does this hold for all VB.NET attributes?

Upvotes: 2

Views: 45

Answers (1)

There is no difference. From MSDN:

By convention, all attribute names end with the word "Attribute" to distinguish them from other items in the .NET Framework. However, you do not need to specify the attribute suffix when using attributes in code.

So, an Attribute written as MyFooAttribute can be used as MyFoo or MyFooAttribute and will show as the same entry in Object Browser. The value in this is to help prevent "name contortions" in a namespace: Newtonsoft has a JsonConverter Type but can also use essentially the same name as an Attribute.

The reverse is not true (at least in VS2012). If you use the 'short form' for a custom attribute:

Public Class MyFoo
    Inherits Attribute

The attribute is legal and usable (owing to its inheritance) but MyFooAttribute wont be recognized in code nor found in Object Browser. So for custom attributes, it pays to name the class as MyFooAttribute.

Upvotes: 3

Related Questions