#23 – C# Preprocessor directives and DefineConstants

In this tutorial, you will learn how to use C# preprocessor directives.

The preprocessor directives give instruction to the compiler to preprocess the information before actual compilation starts.

  • C# preprocessor directive begins with a # (hash) symbol.
  • A preprocessor directive must be the only instruction on a line.
  • C# Preprocessor directives are not statements, so they do not end with a semicolon (;).

C# Preprocessor Directives

  • #define
  • #undef
  • #if
  • #elif
  • #else
  • #endif
  • #warning
  • #error
  • #line
  • #region
  • #endregion
  • #pragma

Conditional Compilation

Use #define to define symbol and #if will evaluate it true if the symbol was defined.

#define HELLO

using System;

#if HELLO

Console.WriteLine("Defined HELLO symbol");

#endif

DefineContants

The DefineConstants option defines symbols in all source code files of your program.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <DefineConstants Condition=" '$(IsTest)' == 'true' ">$(DefineConstants);TEST</DefineConstants>
    <DefineConstants>$(DefineConstants);NEW_TEST</DefineConstants>
  </PropertyGroup>

</Project>

Program.cs

#define HELLO

using System;
using HelloWorld;

#if HELLO

Console.WriteLine("Defined HELLO symbol");

#endif

TestClass test = new TestClass();
test.TestMethod();

TestClass.cs

using System;

namespace HelloWorld
{
    public class TestClass
    {
        public void TestMethod()
        {

#if HELLO
            Console.WriteLine("There is HELLO preprocessor directive.");
#endif

#if TEST
            Console.WriteLine("There is TEST preprocessor directive.");
#else
            Console.WriteLine("There are no TEST preprocessor directives");
#endif


#if DEBUG
            Console.WriteLine("There is DEBUG preprocessor directive.");
#else
            Console.WriteLine("There are no DEBUG preprocessor directives");
#endif


#if TRACE
            Console.WriteLine("There is TRACE preprocessor directive.");
#else
            Console.WriteLine("There are no TRACE preprocessor directives");
#endif


#if NEW_TEST
            Console.WriteLine("There is NEW_TEST preprocessor directive.");
#else
            Console.WriteLine("There are no NEW_TEST preprocessor directives");
#endif

        }
    }
}

Run application with command: dotnet run and get output:

Defined HELLO symbol
There are no TEST preprocessor directives
There is DEBUG preprocessor directive.
There is TRACE preprocessor directive.
There is NEW_TEST preprocessor directive.

Passing Define Constants from dotnet CLI

Run dotnet build command:

dotnet build -p:IsTest=true;

And then run the built dll with command:

 dotnet ./bin/Debug/net5.0/HelloWorld.dll

Output:

Defined HELLO symbol
There is TEST preprocessor directive.
There is DEBUG preprocessor directive.
There is TRACE preprocessor directive.
There is NEW_TEST preprocessor directive.

DefineConstants Variable

Following are another sample for defining a symbol variable in project file, and then pass the symbol from CLI when build.

.csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <DefineConstants Condition=" '$(extraDefineConstants)' != '' ">$(DefineConstants);$(extraDefineConstants)</DefineConstants>
  </PropertyGroup>

</Project>

code snippet:

#define HELLO

using System;

#if PASS_FROM_CLI
    Console.WriteLine("This is passed from extra DefineConstants.");
#else
    Console.WriteLine("This are no passed from extra DefineConstants.");
#endif

build with following command:

dotnet build -p:extraDefineConstants=PASS_FROM_CLI

run with following command:

dotnet ./bin/Debug/net5.0/HelloWorld.dll

Output:

This is passed from extra DefineConstants.

Summary

In this tutorial, you have learn how to use C# preprocessor directives, especially how to use DefineConstants.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.