Information Technology Reference
In-Depth Information
Multiple Attributes
You can apply multiple attributes to a single construct.
Multiple attributes can be listed in either of the following formats:
-
Separate attribute sections stacked on top of each other
-
A single attribute section, with the attributes separated by commas
You can list the attributes in any order.
For example, the following two sections of code show the two ways of applying multiple
attributes. The sections of code are equivalent.
[ Serializable ] // Stacked
[ MyAttribute("Simple class", "Version 3.57") ]
[
MyAttribute("Simple class", "Version 3.57")
, Serializable ] // Commas
↑
↑
Attribute
Attribute
Other Targets
Besides classes, you can also apply attributes to other program constructs such as fields and
properties. The following declaration shows an attribute on a field, and multiple attributes on
a method.
[MyAttribute("Holds a value", "Version 3.2")] // On a field
public int MyField;
[Obsolete] // On a method
[MyAttribute("Prints out a message.", "Version 3.6")]
public void PrintOut()
{
...
You can also explicitly label attributes to apply to a particular construct. To use an explicit
target, place the target type, followed by a colon, at the beginning of the attribute section. For
example, the following code adorns the
method
with an attribute, and also applies an attribute
to the
return value
.