Conditional (C# Programming Guide)

Makes the execution of a method dependent on a preprocessing identifier. The Conditional attribute is an alias for ConditionalAttribute, and can be applied to a method or an attribute class.

In this example, Conditional is applied to a method to enable or disable the display of program-specific diagnostic information:

#define TRACE_ON
using System;
using System.Diagnostics;

public class Trace
{
    [Conditional("TRACE_ON")]
    public static void Msg(string msg)
    {
        Console.WriteLine(msg);
    }
}

public class ProgramClass
{
    static void Main()
    {
        Trace.Msg("Now in Main...");
        Console.WriteLine("Done.");
    }
}

If the TRACE_ON identifier is not defined, no trace output will be displayed.

The Conditional attribute is often used with the DEBUG identifier to enable trace and logging features for debug builds but not in release builds, like this:

[Conditional("DEBUG")]
static void DebugMethod()
{
}

Remarks

When a method marked as conditional is called, the presence or absence of the specified preprocessing symbol determines whether the call is included or omitted. If the symbol is defined, the call is included; otherwise, the call is omitted. Using Conditional is a cleaner, more elegant, and less error-prone alternative to enclosing methods inside #if and #endif, like this:

#if DEBUG
void ConditionalMethod()
{
}
#endif

A conditional method must be a method in a class or struct declaration and must have a return type of void.

Using multiple identifiers

If a method has multiple Conditional attributes, a call to the method is included if at least one of the conditional symbols is defined (in other words, the symbols are logically ORed together). In this example, the presence of either A or B will result in a method call:

[Conditional("A"), Conditional("B")]
static void DoIfAorB()
{
    // ...
}

To achieve the effect of logically ANDing symbols, you can define serial conditional methods. For example, the second method below will execute only if both A and B are defined:

[Conditional("A")]
static void DoIfA()
{
    DoIfAandB();
}

[Conditional("B")]
static void DoIfAandB()
{
    // Code to execute when both A and B are defined...
}

Using Conditional with Attribute Classes

The Conditional attribute can also be applied to an attribute class definition. In this example, the custom attribute Documentation will only add information to the metadata if DEBUG is defined.

[Conditional("DEBUG")]
public class Documentation : System.Attribute
{
    string text;

    public Documentation(string text)
    {
        this.text = text;
    }
}

class SampleClass
{
    // This attribute will only be included if DEBUG is defined.
    [Documentation("This method displays an integer.")]
    static void DoWork(int i)
    {
        System.Console.WriteLine(i.ToString());
    }
}

See Also

Concepts

C# Programming Guide

Reference

Reflection (C# Programming Guide)

Attributes (C# Programming Guide)

Disambiguating Attribute Targets (C# Programming Guide)

Creating Custom Attributes (C# Programming Guide)

Accessing Attributes With Reflection (C# Programming Guide)

System.Reflection

Attribute