BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Debugging
.NET 1.1+

C# Conditional Methods

Applications developed using C# and the .NET framework can include conditional methods. These methods are called only when specified compiler symbols have been defined. This makes them ideal for code used for tracing and debugging purposes.

What are Conditional Methods?

A condition method is a method that is decorated with the Conditional attribute and the name of a compiler symbol. If the specified compiler symbol has been defined, the method will be called when the code is executed. If the identifier is not defined, any calls to the method will be stripped from the compiled assemblies.

The most common uses of conditional methods are for tracing and debugging. In these cases, the conditional method is usually linked to the DEBUG symbol, which is automatically included for assemblies that are compiled in debug mode but excluded from release mode builds. Code that outputs trace information can then be included in the conditional methods, which will not be called in the final production system.

Conditional methods provide similar benefits to code that is excluded using the #if compiler directive. However, there is a very important difference between the two methods. If a section of code is surrounded by #if and #endif compiler directives, this code will not be included in the build unless the specified symbol is defined. With a conditional method, the method is always compiled into the final program. If the specified identifier is not present the method is still included in the final assembly but any calls to that method are not.

This key difference has benefits and drawbacks. The obvious drawback is that the compiled assembly will be larger than if the conditional method were not included. The main benefit is that the assembly can be shared between programs and libraries. Each of these may be compiled with different compiler symbols defined. This means that a single assembly can behave differently according to the program calling it. Another benefit is that the main code of the program is seen by some as more elegant without the #if and #endif conditions.

Conditional Attribute

The Conditional attribute can be applied to methods that have a return type of void. The attribute requires a single parameter, which is a string containing the name of the compiler symbol that must be present in order for the method to be called. The following code shows a method that is only called if the DEBUG symbol is present.

[Conditional("DEBUG")]
public static void OutputTraceInformation()
{
    Console.WriteLine("Trace information");
}

As the DEBUG identifier is specified in the attribute, we can test the conditional method by switching between debug and release modes. Add the above method to a console application and then add the line below to the Main method. When compiled in debug mode the program will output the message. In release mode, the program will output nothing.

OutputTraceInformation();
7 October 2008