An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for.
Attributes are used for adding metadata, such as compiler instruction and other information such as comments, description, methods and classes to a program. The .Net Framework provides two types of attributes: the pre-defined attributes and custom built attributes.
Name of the attribute and its values are specified within the square brackets, before the element to which the attribute is applied. Positional parameters specify the essential information and the name parameters specify the optional information.
Where,
For example,
The following example demonstrates the attribute −
When the above code is compiled and executed, it produces the following result −
Where,
When you try to compile the program, the compiler gives an error message stating −
In the preceding code, we have declared a custom attribute named DeBugInfo.
In the next chapter, we retrieve attribute information using a Reflection class object.
Specifying an Attribute
Syntax for specifying an attribute is as follows −shellscript
Predefined Attributes
The .Net Framework provides three pre-defined attributes −- AttributeUsage
- Conditional
- Obsolete
AttributeUsage
The pre-defined attribute AttributeUsage describes how a custom attribute class can be used. It specifies the types of items to which the attribute can be applied. Syntax for specifying this attribute is as follows −csharp
- The parameter validon specifies the language elements on which the attribute can be placed. It is a combination of the value of an enumerator AttributeTargets. The default value is AttributeTargets.All.
- The parameter allowmultiple (optional) provides value for the AllowMultiple property of this attribute, a Boolean value. If this is true, the attribute is multiuse. The default is false (single-use).
- The parameter inherited (optional) provides value for the Inheritedproperty of this attribute, a Boolean value. If it is true, the attribute is inherited by derived classes. The default value is false (not inherited).
csharp
Conditional
This predefined attribute marks a conditional method whose execution depends on a specified preprocessing identifier. It causes conditional compilation of method calls, depending on the specified value such as Debug or Trace. For example, it displays the values of the variables while debugging a code. Syntax for specifying this attribute is as follows −shellscript
csharp
csharp
shellscript
Obsolete
This predefined attribute marks a program entity that should not be used. It enables you to inform the compiler to discard a particular target element. For example, when a new method is being used in a class and if you still want to retain the old method in the class, you may mark it as obsolete by displaying a message the new method should be used, instead of the old method. Syntax for specifying this attribute is as follows −shellscript
- The parameter message, is a string describing the reason why the item is obsolete and what to use instead.
- The parameter iserror, is a Boolean value. If its value is true, the compiler should treat the use of the item as an error. Default value is false (compiler generates a warning).
csharp
csharp
Creating Custom Attributes
The .Net Framework allows creation of custom attributes that can be used to store declarative information and can be retrieved at run-time. This information can be related to any target element depending upon the design criteria and application need. Creating and using custom attributes involve four steps −- Declaring a custom attribute
- Constructing the custom attribute
- Apply the custom attribute on a target program element
- Accessing Attributes Through Reflection
Declaring a Custom Attribute
A new custom attribute should is derived from the System.Attribute class. For example,csharp
Constructing the Custom Attribute
Let us construct a custom attribute named DeBugInfo, which stores the information obtained by debugging any program. Let it store the following information −- The code number for the bug
- Name of the developer who identified the bug
- Date of last review of the code
- A string message for storing the developer's remarks
csharp
Applying the Custom Attribute
The attribute is applied by placing it immediately before its target −csharp

