System.Diagnostics.ConditionalAttribute Class

Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.

See Also: ConditionalAttribute Members


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


You can apply the System.Diagnostics.ConditionalAttribute attribute to methods and classes. However, its use on classes is valid only for types that are derived from Attribute. System.Diagnostics.ConditionalAttribute either will be ignored or will produce a compiler warning or error message if you apply it to any other type.

Applying System.Diagnostics.ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with System.Diagnostics.ConditionalAttribute is defined. You will get a compilation error in Visual Studio if you apply this attribute to a method that does not return void. Applying System.Diagnostics.ConditionalAttribute to an attribute indicates that the attribute should not be emitted to metadata unless the conditional compilation symbol is defined. Any arguments passed to the method or attribute are still type-checked by the compiler.

You can use the following techniques to define conditional compilation symbols:

Compilers that comply with the Common Language Specification (CLS) are permitted to ignore System.Diagnostics.ConditionalAttribute. The C#, Visual Basic, and C++ compilers support System.Diagnostics.ConditionalAttribute; the JScript compiler does not support the attribute.


In Visual Basic, the AddressOf operator is not affected by this attribute. For example, Call CType(AddressOf delegate, Action) always invokes delegate, although Call delegate() might not.

System.Diagnostics.ConditionalAttribute is applied to the methods that are defined in the System.Diagnostics.Debug and System.Diagnostics.Trace classes.

For more information about how to use attributes, see Extending Metadata Using Attributes.

Thread Safety

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


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 { 

  public static void Display() { 

    Console.WriteLine("Compiled with DEBUG"); 

public class TestCondition { 

  public static void Main() { 

    Console.WriteLine("How was this compiled?"); 

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

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

How was this compiled?


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