Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ConditionalAttribute Class

Indicates to compilers that a method is callable if a specified preprocessing identifier is applied to the method.

Namespace: System.Diagnostics
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple=true)] 
[ComVisibleAttribute(true)] 
public sealed class ConditionalAttribute : Attribute
/** @attribute SerializableAttribute() */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple=true) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class ConditionalAttribute extends Attribute
SerializableAttribute 
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple=true) 
ComVisibleAttribute(true) 
public final class ConditionalAttribute extends Attribute

The ConditionalAttribute supports the conditional methods defined in the Trace and Debug classes.

Methods decorated with the ConditionalAttribute attribute are always compiled into Microsoft intermediate language (MSIL), but calls to the methods can not be made at run time. If the method has arguments, they are type-checked at run time, but not evaluated.

NoteNote

Visual Studio 2005 compiles Release builds with the TRACE conditional compilation constant defined, and Debug builds with both the DEBUG and TRACE constants defined, by default. For command-line builds, you must specify all conditional compilation constants.

NoteNote

A ConditionalAttribute that has an associated ConditionString can be attached to the definition of a method, creating a conditional method. Thereafter, when a compiler encounters a call to that method, it can 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 ConditionString supplied to the ConditionalAttribute.

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

  • 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 the ConditionalAttribute.

For more information about using attributes, see Extending Metadata Using Attributes.

The following console application example demonstrates the use of ConditionalAttribute with a particular compiler that supports the use of this attribute.

using System;
using System.Diagnostics;

class Class1
{
	[STAThread]
	static void Main(string[] args)
	{				
		TextWriterTraceListener myWriter = 
			new TextWriterTraceListener(System.Console.Out);
		Debug.Listeners.Add(myWriter);
		Console.WriteLine("Console.WriteLine is always displayed");
		Method1();
		Method2();
	}
	
	[Conditional("CONDITION1")]
	public static void Method1()
	{
		Debug.Write("Method1 - DEBUG and CONDITION1 are specified\n");
		Trace.Write("Method1 - TRACE and CONDITION1 are specified\n");
	}
	
	[Conditional("CONDITION1"), Conditional("CONDITION2")]	
	public static void Method2()
	{
		Debug.Write("Method2 - DEBUG, CONDITION1 or CONDITION2 are specified\n");
	}
}

/*
This console application produces the following output when compiled as shown.

Console.WriteLine is always displayed
Method1 - DEBUG and CONDITION1 are specified
Method1 - TRACE and CONDITION1 are specified
Method2 - DEBUG, CONDITION1 or CONDITION2 are specified

Console.WriteLine is always displayed
Method2 - DEBUG, CONDITION1 or CONDITION2 are specified

Console.WriteLine is always displayed
Method1 - TRACE and CONDITION1 are specified

*/

System.Object
   System.Attribute
    System.Diagnostics.ConditionalAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft