More about Reserved Attributes of C# (C Sharp)
are used to append declarative information to your programs. This information
is interpreted during execution and appropriate action is taken. C# provides
ready-to-use attributes called reserved attributes. There are three reserved
attributes in C# : AttributeUsage, Conditional, Obsolete. This article
will help you in understanding these three attributes.
For all attributes derived from System.Attribute, you can define their visibility and accessibility using AttributeUsage. AttributeUsage accepts three parameters which are mentioned below:
Defines which all program entities can use this attribute
AllowMultiple = true, Inherited=false)]
In this example, AttributeUsage definition means that the sampleAttribute is accessible only from classes and methods. Since AllowMultiple is set to true, multiple values can be given to the same attribute associated with either class or method.
Since inherited is false, attribute value set at base class cannot be inherited to derived class. SampleAttrUsageClass uses sampleAttribute and defines value to its parameter. It has provided one value for the attribute. If you look at sampleMethod, it defines two values for the attribute. This is legal because AllowMultiple is set to true in the definition of sampleAttribute.
Conditional attribute can be applied on a method belonging to a class or struct. This attribute determines whether the method (to which it is associated) has to be executed based on the compilation symbol associated with this attribute. Consider the example given below:
In this example sampleMethod of sampleClass1 will be executed only if the symbol TEST is defined using the pre-processor directive #define. Since it is defined, the method will be executed when it is invoked in Main( ) method of sampleClass2. If the symbol is not defined or the following line is included:
Then this method will not be executed.
Note that conditional attributes cannot be applied on:
with override modifier
If you feel that any specific programming entity (like class, method, property, event…) in your code is no longer required and it need not be used, what will you do? You will remove or comment the programming entity from your code and remove/comment all its usage. What if you need the same function at a later point of time? What if you forgot to remove few lines of code?
You can avoid all those confusions that might arise by using obsolete attribute. Any entity that is marked as obsolete can no longer be used. If it is accessed anywhere in your code, then either a warning message or an error will be thrown. You have the control of deciding whether warning message or error has to be prompted. At a later point of time if you want to use the entity, then all that you have to do is: remove the obsolete attribute associated with it. Here’s an example:
In this example, sampleMethod1 and sampleMethod2 are marked as obsolete. When you look at the two obsolete attributes above, the obsolete attribute associated with sampleMethod1 has two parameters. First parameter contains the message that has to be displayed when sampleMethod1 is accessed. Second parameter is optional. It accepts Boolean value.
When the second parameter is set to true, error message is thrown to user if this method is accessed. When the parameter is not mentioned or it is set to false, warning message is shown to user. In this example, sampleMethod3 doesn’t have any obsolete attribute associated it, hence it will be executed. On accessing sampleMethod2, a warning message is prompted and on accessing sampleMethod1, an error is thrown.
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
|Designing applications in .Net using Service Oriented Architecture | How to Create and Use Anonymous Delegates in C# (C Sharp) |How to Define Custom Attributes in C# (C Sharp)| How To Handle Errors While Developing ASP.NET Applications | How to Use Indexer in Classes and Interfaces of C# (C Sharp)| Illustration of Access Keywords (base, this) with Examples in C# (C Sharp) | Illustration of Null Coalesce Operator (??) in C# (C Sharp) |Indexers Vs Properties in C# (C Sharp) | More about Reserved Attributes of C# (C Sharp)| Overview of Unified Type System of C# (C Sharp) | Purpose of Delegates in C# (C Sharp) |How does Simple Object Access Protocol (SOAP) help in Internet Communication |How to Create and Use Anonymous Delegates in C# (C Sharp)|