The C# Attributes Series: Conditional Compilation with Attributes

Fine-grained conditional compilation using Conditional attribute

Jan 22, 2024


The C# Attributes Series: Conditional Compilation with Attributes
The C# Attributes Series: Conditional Compilation with Attributes

The Series: Useful C# Attributes for Great Developer UX and Compiler Happiness

This article is part of the Useful C# Attributes for Great Developer UX and Compiler Happiness series. You can find the complete list of articles in the series at the end.


Conditional compilation in .NET (and in other programming languages that support it) is a powerful and flexible mechanism that allows developers to include or exclude parts of the code based on certain conditions.

This capability is not only useful but often essential in managing complex projects with multiple build configurations and diverse deployment targets. By leveraging conditional compilation, developers can significantly reduce the complexity of their code management, ensuring that each build contains only what is necessary for its specific purpose.

Below are some of the scenarios where conditional compilation proves to be particularly valuable:

Platform-Specific Code

Conditional compilation is handy for writing platform or feature-specific code, such as code that should only compile for a Windows build or a particular version of .NET.

Debugging and Diagnostics

One common use of ConditionalAttribute is for including debugging and diagnostics code that should not be present in production.

Enabling Experimental Features

It can be used to enable experimental features in a controlled manner, allowing features to be tested without affecting the main codebase.

Common Way of Conditional Compilation in C#

The most used and traditional way for conditional compilation in C# is by using preprocessor directives like #if, #elif, #else, and #endif. This approach allows you to include or exclude code blocks based on certain conditions, typically defined by symbols. These directives evaluate conditions at compile-time, making them a powerful tool for creating flexible and environment-specific code segments.

In most common cases, those look like this:

Console.WriteLine("Debug mode");
Console.WriteLine("Release mode");
Console.WriteLine("Other configuration");

This approach is acceptable; however, it may lead to cumbersome and unsightly code if overused.

Another method that allows for a more refined strategy, albeit with certain limitations, involves the use of Conditional attributes, which we will explore here.

Understanding Conditional Attribute

ConditionalAttribute, defined in System.Diagnostics, is an attribute that conditions the execution of methods or attribute classes based on the presence of a specified compilation symbol. According to the official documentation, it plays a key role in controlling method execution depending on the defined compilation symbols.


The ConditionalAttribute can be applied to methods and attribute classes.

When applied to a method, the method call is included in compilation only if the specified conditional symbol is defined. It's important to note that such methods must return void.

When applied to an attribute class, the attribute is omitted unless the specified conditional symbol is defined.

Applying ConditionalAttribute to Methods

ConditionalAttribute applied to methods is a crucial feature in .NET, enabling conditional compilation at the method level. This attribute effectively controls whether a method call is included in the compiled code based on the presence of a specific compilation symbol.

Understanding Conditional Method Execution


When ConditionalAttribute is applied to a method, calls to this method are included or omitted from compilation based on whether the specified conditional compilation symbol is defined.

It’s a powerful tool for creating flexible code that behaves differently under various compilation settings, such as debug versus release modes. With the notion that is looking at the whole method call chain, there are really interesting possibilities for usage using this attribute.

Usage in Methods

Key Requirements

  • Return Type: Methods marked with ConditionalAttribute must return void.

  • Effect on Calls: The method's body remains intact, but calls to the method are included or excluded based on the defined symbol. If the symbol is not defined, calls to the method are omitted, as if they were commented out.


public void DebugLog(string message)
    Console.WriteLine("Debug: " + message);

public void ProcessData()
    DebugLog("Processing started");
    // Further processing logic...

In this example, the DebugLog method is called within ProcessData. If the DEBUG symbol is defined, the call to DebugLog is included and executed. If DEBUG is not defined, the call to DebugLog in ProcessData is omitted during compilation.

Applying Conditional Attribute to Attribute Classes

While ConditionalAttribute is commonly known for its use on methods, its application on attribute classes is less frequently discussed but equally powerful. This feature can be especially useful in scenarios where the inclusion of certain attributes should depend on compilation symbols, often used for debugging, logging, or differentiating between various build configurations.

Understanding the application to Attribute Classes


When applied to an attribute class, ConditionalAttribute dictates that the attribute's usage is conditional. The presence of the attribute on a class, method, or property depends on whether a specific compilation symbol is defined. This conditional compilation of attributes can have significant implications, particularly in how it affects other features that rely on these attributes.


The primary consideration when applying ConditionalAttribute to an attribute, class understands the compilation symbol's impact. The attribute class will only be effective when the specified conditional compilation symbol is defined. If the symbol is not defined, it's as if the attribute were not present at all.


public class CustomLogAttribute : Attribute
    // Implementation of the attribute

public void MethodWithConditionalLogging()
    // Method implementation

In this example, CustomLogAttribute is marked with Conditional("CUSTOM_LOGGING"). If the CUSTOM_LOGGING symbol is defined, then MethodWithConditionalLogging will be annotated with CustomLogAttribute.

If CUSTOM_LOGGING is not defined, CustomLogAttribute will not be applied to the method.

Considerations and Implications

Impact on Reflection

  • Reflection Behavior: The conditional application of attributes can impact how reflection works. When the conditional symbol is not defined, reflection code that checks for these attributes will behave differently, as the attributes will be absent.

Debugging and Diagnostics

  • Debugging Scenarios: This feature is useful in debugging scenarios where certain attributes (like logging or profiling attributes) should only be active in debug builds.

Code Readability and Maintenance

  • Maintainability: While powerful, conditional attributes can make the code harder to understand and maintain, as the behavior of the code might change based on the build configuration.

  • Documentation: Clear documentation and comments are essential to explain why and how conditional attributes are used, helping other developers understand the varying behaviors in different build configurations.

Best Practices

  • Selective Use: Use conditional attributes judiciously and only when the benefits outweigh the potential confusion or maintenance overhead.

  • Testing: Ensure that all configurations are thoroughly tested, considering both the presence and absence of these conditional attributes.

  • Clear Compilation Symbols: Use meaningful names for compilation symbols to clearly indicate their purpose and impact.

Addendum: Defining Conditional Symbols

In Code with Preprocessor Directives

You can define or undefine conditional symbols in the code using preprocessor directives.

#define DEBUG// Code here...#undef DEBUG

In .csproj File

Alternatively, conditional symbols can be defined in the .csproj file.


In .csproj but with Variables

Conditional compilation in C# can be controlled not just by simple symbols but also through more sophisticated configurations involving variables in .csproj files. This method enhances flexibility and allows for more dynamic control of the compilation process.

Setting Up Conditional Compilation Symbols in .csproj

In the .csproj file, you can define conditional compilation symbols based on various conditions such as configuration (Debug/Release) and platform. These symbols can then be used within your code to include or exclude code blocks conditionally. By using variables, you can create more complex and configurable setups.

Example Configuration

<PropertyGroup><!-- Define a variable to control guard checks in release mode --><ExcludeGuardChecksInRelease>false</ExcludeGuardChecksInRelease></PropertyGroup>

<!-- Conditional Compilation Symbol based on the variable -->
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' And '$(Six_ExcludeGuardChecksInRelease)' == 'true' ">

In this setup, a variable ExcludeGuardChecksInRelease is defined, and a conditional compilation symbol EXCLUDE_GUARD_CHECKS_IN_RELEASE is set based on this variable.

Using Conditional Compilation Symbols in Code

Example Usage

public void SomeMethod()
    Guard.CheckNotNull(argument, nameof(argument));
    #endif// Method implementation...

In this code snippet, the Guard.CheckNotNull method is called conditionally based on the EXCLUDE_GUARD_CHECKS_IN_RELEASE symbol. If the symbol is defined (which happens based on the variable in .csproj), the guard check is excluded.


  • Flexibility: Allows different behaviors for different build configurations or environments without changing the source code.

  • Maintainability: Keeps the codebase cleaner by avoiding numerous #if directives spread throughout the code.

  • Configurability: Enables toggling features on and off for different builds by simply changing variables in the .csproj file.

Note About Usage in Referenced Projects

When you have multiple projects, and one references another, ensure that the symbols are defined consistently across these projects.

If a project references another project with certain conditional compilation symbols, the referencing project needs to define the same symbols for consistent behavior.

Cautions and Best Practices

  • Readability and Maintenance: Overusing conditional compilation can lead to code that is hard to read and maintain. It should be used judiciously.

  • Testing: Ensure that all code configurations are thoroughly tested. Code that is conditionally compiled might not be tested as frequently.

  • Documentation: Maintain clear documentation about the purpose and usage of conditional symbols in the codebase.

What Have We Learned?

In our exploration of "Conditional Compilation with Attributes" in the .NET ecosystem, we have covered several essential concepts and techniques. Here’s a recap of the key topics and the valuable insights gained:

The Power and Flexibility of Conditional Compilation

  • Insight: Conditional compilation is an essential tool in managing complex projects with diverse build configurations and deployment targets.

  • Learnings: We've seen how it can be used to tailor code for specific platforms, include debugging and diagnostics code, and manage experimental features.

Traditional Conditional Compilation with Preprocessor Directives

  • Insight: Preprocessor directives like #if, #elif, #else, and #endif are fundamental tools for including or excluding code based on defined symbols.

  • Learnings: We've learned how these directives allow for compile-time decision-making, though we also noted the potential for code clutter if overused.

ConditionalAttribute for Methods and Attribute Classes

  • Insight: ConditionalAttribute offers a refined approach to conditional compilation, applying to both methods and attribute classes.

  • Learnings: We've seen how this attribute can conditionally include method calls and attribute usage in the compilation process based on specified symbols. This leads to cleaner, more readable code compared to traditional preprocessor directives.

Defining Conditional Symbols

  • Insight: Conditional symbols can be defined both in code (using preprocessor directives) and in .csproj files, with the latter offering enhanced flexibility through variables.

  • Learnings: We explored how to set up and use these symbols in .csproj files, enhancing configurability and maintainability of complex projects.

Best Practices and Considerations

  • Insight: While conditional compilation is powerful, it requires careful management to ensure code readability, maintainability, and thorough testing across all configurations.

  • Learnings: We emphasized the importance of documentation, consistent symbol definitions across referenced projects, and judicious use of conditional compilation to avoid unnecessary complexity.


Through this article, we have gained a comprehensive understanding of how to use conditional compilation in .NET. effectively. We've seen its versatility in handling various development scenarios, from debugging to platform-specific code. The key is to use these techniques judiciously and thoughtfully, ensuring that they serve to enhance, rather than complicate, the development process.

Useful C# Attributes for Great Developer UX and Compiler Happiness Series

This article is part of the Useful C# Attributes for Great Developer UX and Compiler Happiness series. If you enjoyed this one and want more, here is the complete list in the series:

Happy Coding!

More Articles

for Visiting

.. and now that you've scrolled down here, maybe I can invite you to explore other sections of this site

for Visiting

.. and now that you've scrolled down here, maybe I can invite you to explore other sections of this site