RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Get Personal with C# Custom Attributes

C# attributes may seem like an insignificant element of C# grammar, but did you know they provide a powerful way to extend your metadata? This goes a long way towards simplifying component-based programming. Here's what you need to know about custom attributesand how to use them.

henever I bump into one of my Java friends, he invariably tells me C# is no better than Java. I generally reply that one of my favorite features of C# is custom attributes. This tends to cut the Java versus C# debate short, since it's clear that custom attributes are very useful and dearly missed in Java. Indeed, while attributes may seem like a relatively insignificant element of C# grammar, they can dramatically ease your development. This article describes what custom attributes are, the different kind of attributes, and creative ways to use them.

Custom Attributes Defined
In source code-based programming, you annotate your code using comments. This is just fine because whoever reuses your code will see these annotations. However, in component-based programming, the metadata replaces the source file as the method of reuse. Custom attributes are extensions of the metadata that survive compilation and are therefore visible by the client even if he doesn't have access to the source code.

Custom attributes differ from regular code. While regular code describes what the execution should do, attributes state something about the software itself.

Using Custom Attributes
When it is clear from the syntax what the attribute applies to, you just apply it between brackets before the element to annotate. For example, the following code fragment declares four attributes at the class, field, method and parameter level:

class SomeClass
	int SomeField;
	int SomeMethod(
		[AttributeForTheParameter]int someParam)
When the C# syntax does not clearly present the element to annotate, you need to add the name of the target in the attribute usage. The name of the target is one of the following:

Target name



The attribute applies to the entire assembly.


The attribute applies to the entire module.


The attribute applies to the return value of the method, property set branch or indexer set branch.


The attribute applies to the implicit "value" parameter of the property or indexer.

Table 1: Attribute Targets.

The next code fragment first declares two global attributes--one for the entire assembly and one for the entire module. The return value of SomeMethod also has a custom attribute. Note that without the "return:" target indicator, the attribute would apply to the method itself.

[assembly: AttributeForTheEntireAssembly]
[module: AttributeForTheEntireModule]
class SomeClass
	[return: AttributeForTheReturnValue]
	int SomeMethod(){...}
	int SomeProperty
		[return: AttributeForTheReturnValue]
			return ...;
		[value: AttributeForTheValue]
			... = value;
This same principle applies to the get methods of properties. The property set method's "value" implicit parameter has a custom attribute. Again, without the "value:" target indicator, the attribute would apply to the set method itself.

Attributes themselves implement as regular .NET classes. In particular, they are able to have members. Differentiate attributes from other .NET classes by deriving them from System.Attribute.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date