Attributes in C#
Feb 20, 2018 01:00 0 Comments C# (C-Sharp) Sashi

                        Attributes in C#

An attribute is a declarative tag that is used to convey information to runtime about the behaviours 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.


Specifying an Attribute

Syntax for specifying an attribute is as follows −

[attribute(positional_parameters, name_parameter = value, ...)]


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.


Predefined Attributes

The .Net Framework provides three pre-defined attributes −

  • AttributeUsage
  • Conditional
  • Obsolete



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 −

[AttributeUsage (


   AllowMultiple = allowmultiple,

   Inherited = inherited



·        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 Inherited property of this attribute, a Boolean value. If it is true, the attribute is inherited by derived classes. The default value is false (not inherited).

For example,


   AttributeTargets.Class |

   AttributeTargets.Constructor |

   AttributeTargets.Field |

   AttributeTargets.Method |


   AllowMultiple = true)]



This predefined attribute marks a conditional method whose execution depends on a specified pre-processing 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 −





For example,


 The following example demonstrates the attribute −

#define DEBUG

using System;

using System.Diagnostics;

 public class Myclass {


     public static void Message(string msg) {




Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.

Let`s Connect