
More about Reserved Attributes of C# (C Sharp) Attributes
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.
AttributeUsage: For all attributes
derived from System.Attribute, you can define their visibility and accessibility
using AttributeUsage. AttributeUsage accepts three parameters which are
mentioned below: validon:
Defines which all program entities can use this attribute [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,
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: 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: #define TEST 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: #undef TEST Then this
method will not be executed. Note that
conditional attributes cannot be applied on: Methods
with override modifier Obsolete: 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. Heres an example: class sampleClass1 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
doesnt 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.
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |