This API supports the product infrastructure and is not intended to be used directly from your code.
Returns a value that indicates whether this instance is equal to a specified object.(Inherited from Attribute.)

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

Applying 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 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 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:

Use compiler command-line options; for example, /define:DEBUG.

Use environment variables in the operating system shell; for example, set DEBUG=1.

Use pragmas in the source code; for example, define the compilation variable as follows:

No code example is currently available or this language may not be supported.

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

Note

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.

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

The following example demonstrates the use of ConditionalAttribute. The example assumes that the condition is defined with the /define compiler option. You can obtain different results by changing the compiler option. You can optionally define the conditions by using pragmas in the sample code instead of identifying them as compiler options.