Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# custom attribute naming

I have a custom Attribute class that I defined as:

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class MyCustomAttribute : System.Attribute
{
    ...
}

From the microsoft website:

By convention, the name of the attribute class ends with the word Attribute. While not required, this convention is recommended for readability. When the attribute is applied, the inclusion of the word Attribute is optional.

So, the attribute can be use by either

[MyCustom()]

or

[MyCustomAttribute()]

My question to you all, is if anyone has experienced any problems with using the abbreviated version of the name vs the full name? I am running 4.0 framework.

Thanks!

like image 571
JustBrowsing Avatar asked Apr 01 '11 13:04

JustBrowsing


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


2 Answers

No problems.

Underneath in compiled IL the name always has Attribute appended to it (the full type name).

Based on ECMA-334 (C# spec) 24.2, the resolution is done first by exact match, then by appending "Attribute" to the name given in []. Additionally, if there is a conflict (ex: MyAttribute, and MyAttributeAttribute) a compile time error is generated when "MyAttribute" would be used.

like image 137
earlNameless Avatar answered Oct 09 '22 02:10

earlNameless


It should be fine... I'd just advise you not to introduce two attributes with names only differing by the number of Attribute suffixes:

public class FooAttribute : Attribute { }

public class FooAttributeAttribute : Attribute { }

[FooAttribute] // Could be either!

I suspect the exact match would win here, but please don't introduce the ambiguity in the first place. (I haven't checked the spec.)

like image 23
Jon Skeet Avatar answered Oct 09 '22 02:10

Jon Skeet