Builtin Attributes


There are a number of built-in attributes. The three that will be examined in this topic are:

  1. AttributeUsage
  2. Conditional
  3. Obsolete

AttributeUsage

The AttributeUsage attribute specifies the types of items to which an attribute may be applied. AttributeUsage is another name for System.AttributeUsageAttribute. The AttributeUsage class has the following constructor:

AttributeUsage(AttributeTargets targets)

where targets specifies the item or items to which the attribute may be attached. AttributeTargets is an enumeration with the following members.

All Assembly Class Constructor
Delegate Enum Event Field
GenericParameter Interface Method Module
Parameter Property ReturnValue Struct

Enumerators may be combined using the or operator. For example, to define an attribute that applies to both fields and properties, the following may be used.

AttributeTargets.Field | AttributeTargets.Property

AttributeUsage supports two named parameters:

The AllowMultiple named parameter is a bool value. When true, the attribute can be applied more than one time to a single item. Inherited is also a bool value such that when it is true, the attribute is inherited by derived classes (otherwise it is not). The default setting is false for AllowMultiple and true for Inherited.

AttributeUsage also possesses a read-only property called ValidOn. ValidOn returns a value of type AttributeTargets, indicating the types of items to which the attribute may be applied.

The Conditional Attribute

The conditional attribute is used for conditional processing. It has the general form:

[Conditional(symbol)]

where symbol is the symbol that determines whether the method will be executed. When the conditional attribute is applied to a method, that method is called only when a certain symbol has been defined through #define. A method to which the conditional attribute has been applied is referred to as a conditional method. Conditional methods provide an alternative to conditional compilation with the directive #if. The conditional attribute is called Conditional, which is another name for System.Diagnostics.ConditionalAttribute. To use the conditional attribute, the namespace System.Diagnostics should be included in the program.

To illustrate the use of the conditional attribute, the following program is made available.

// Runtime12 - The Conditional Attribute

#define Symbol1

using System;
using System.Diagnostics;

class Program
{
    [Conditional("Symbol1")]
    static void TestMethod1()
    {
        Console.WriteLine("In TestMethod1");
    }

    [Conditional("Symbol2")]
    static void TestMethod2()
    {
        Console.WriteLine("In TestMethod2");
    }

    static void Main(string[] args)
    {
        TestMethod1();
        TestMethod2();
    }
}

The output of this program is as follows.

In TestMethod1

TestMethod1() is made conditional upon Symbol1 and TestMethod2() is made conditional up Symbol2. Only Symbol1 is defined, so the call to TestMethod1() succeeds while the call to TestMethod2() is bypassed.

Restrictions on the use of the conditional attribute are:

  1. the method must return void,
  2. the method cannot be the member of an interface and
  3. the method cannot be an override.

The Obselete Attribute

The obselete attribute attribute has the general form:

[Obselete("message")]

where message is a message displayed when the program element is compiled. A second form of Obselete is shown below.

[Obselete("message",error)]

error is a boolean value that when true causes a compilation error rather than a warning (the default is warning).