Mono Class Library: System.Diagnostics Namespace

System.Diagnostics.ConditionalAttribute Class

Indicates to compilers that a method is callable if and only if a specified pre-processing identifier has been defined on the method. [Edit]

See Also: ConditionalAttribute Members

System.Object
     System.Attribute
          System.Diagnostics.ConditionalAttribute

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method | System.AttributeTargets.All, AllowMultiple=true)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ConditionalAttribute : Attribute

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Remarks

Note:

A System.Diagnostics.ConditionalAttribute, which has an associated condition ConditionalAttribute.ConditionString, can be attached to the definition of a method, creating a conditionalmethod . Thereafter, when a compiler encounters a call to that method, it might choose to ignore the call unless a compilation variable is defined at the site of the call, with a value that matches in a case-sensitive manner the ConditionalAttribute.ConditionString supplied to the System.Diagnostics.ConditionalAttribute .

Note that compilers might provide several techniques to define such compilation variables, such as:

  • compiler command-line switches (for example, /define:DEBUG )
  • environment variables in the operating system shell (for example, SET DEBUG=1)
  • as pragmas in the source code (for example, #define DEBUG, to define the compilation variable, or #undef DEBUG to undefine it)

CLS-Compliant compilers are permitted to ignore uses of the System.Diagnostics.ConditionalAttribute .

[Edit]

Example

The following example demonstrates the use of System.Diagnostics.ConditionalAttribute with a particular compiler that supports the use of this attribute. The ConditionalAttribute.ConditionString property of the current attribute is initialized as "DEBUG".

C# Example
using System;
using System.Diagnostics;

public class MyClass { 

  [ConditionalAttribute("DEBUG")] 
  public static void Display() { 

    Console.WriteLine("Compiled with DEBUG"); 
  }
}

public class TestCondition { 

  public static void Main() { 

    Console.WriteLine("How was this compiled?"); 
    MyClass.Display(); 
    Console.WriteLine("<eop>"); 
  }
}
      

When this code is compiled with the compilation-variable DEBUG defined at the callsite, the output when run is

How was this compiled?
Compiled with DEBUG
<eop>

When this code is compiled without the compilation-variable DEBUG defined at the callsite, the output when run is

How was this compiled?
<eop>

Requirements

Namespace: System.Diagnostics
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0