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
• AllowMultiple: When this parameter is set to true, multiple values can be set to the attribute when it is associated with an entity
• Inherited: When it is set to true, derived classes can inherit the attributes used in the base class

Here is a simple definition using AttributeUsage:

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple = true, Inherited=false)]
public class sampleAttribute : System.Attribute {
public string sampleParam1;
public sampleCustomAttribute(string sampleParam) {
this.sampleParam1= sampleParam;
}

}
[sampleAttribute("testvalue1”)]
class SampleAttrUsageClass {
[sampleAttribute("testmethodvalue1”)]
[sampleAttribute("testmethodvalue2”)]
public void sampleMethod() { }
}

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
using System;
using System.Diagnostics;
class sampleClass1
{
[Conditional("TEST")]
public void sampleMethod() {
Console.WriteLine("sampleClass1.sampleMethod is executed");
}
}
class sampleClass2
{
public static void Main() {
sampleClass1 sampleObj = new sampleClass1();
sampleObj.sampleMethod();
}
}

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
Instead of
#define TEST

Then this method will not be executed.

Note that conditional attributes cannot be applied on:

• Methods with override modifier
• Methods belonging to interface
• Method implementing an interface method

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. Here’s an example:

class sampleClass1
{
[Obsolete("sampleMethod1 is obsolete. Avoid using it.”, true)]
public void sampleMethod1() {
Console.WriteLine(“Executing sampleMethod1…”);
}
[Obsolete("sampleMethod2 is obsolete. Avoid using it.”)]
public void sampleMethod2() {
Console.WriteLine(“Executing sampleMethod2…”);
}
public void sampleMethod3(){
Console.WriteLine(“Executing sampleMethod3…”);
}
}
class testSampleClass
{
public static void Main() {
sampleClass1 sampleObj = new sampleClass1();
sampleObj.sampleMethod3();
sampleObj.sampleMethod2();
sampleObj.sampleMethod1();
}
}

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.

|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)|

 


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.