0

I'm trying to understand how #if statement works on C# and how it is used.

After reading some documentation, I have got to the conclusion that it is used to compile the code in it if the "symbol" in the condition is defined.

#if DEBUG
    Console.WriteLine("Debug version");
#endif

The problem here is that I don't know what they mean by "symbol" (in this case, DEBUG), and I'm struggling to find an answer due to the wide meaning of this word.

What are these "symbols" and what is the purpose of these "preprocessor directives"?

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
de12wey
  • 77
  • 1
  • 2
  • 11

5 Answers5

2

As folks already said, it is a pre-processor directive. Code inside the #if DEBUG ... #endif will get conditionally compiled, depending on whether the symbol DEBUG is defined.

In this case, DEBUG is simply a symbolic name introduced specifically for the pre-processor so that it can distinguish blocks of code that should or should not be compiled.

You can define a new pre-processor symbol in the project settings:

Project settings dialog

Here, we have a total of four symbols defined:

  • DEBUG
  • TRACE
  • ANOTHER_SYMBOL
  • YET_ANOTHER_SYMBOL

DEBUG and TRACE are kind of special because they are so widespread. This is why they have dedicated checkboxes. The term "constant" here is used interchangeably with the term "symbol". However, I hear "symbol" more frequently in this particular context.

Another way to define a symbol is through code. You can add #define directives at the very beginning of your .cs source files:

#define YET_ANOTHER_SYMBOL
Kerido
  • 2,930
  • 2
  • 21
  • 34
  • 1
    One advantage of using the project properties is that you can have different symbols for "release" versus "debug" build configurations. – Hans Kesting Apr 01 '19 at 08:40
1

Those are PreProcessor Directives. In your case, it will check if the DEBUG symbol is defined saying #define DEBUG then the piece of code Console.WriteLine will gets executed

#if DEBUG
    Console.WriteLine("Debug version");
#endif
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

As you already assumed the DEBUG-symbol is used by the compiler in order to either translate the code or not. So in a release-build - where DEBUG doesn´t exist - the code is not compiled into IL.

A symbol here means a variable introduced to the compiler. Those are defined in VS Project Properties-->Build-->conditional complation symbols. However there exists a set of pre-defined symbols - such as DEBUG.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

In addition to the predefined, you can also define your own conditional compilation symbol either at the file-level or the project level:

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?redirectedfrom=MSDN&view=netframework-4.7.2

e.g, type this at the start of the file to define "CONDITION1"

#define CONDITION1

or define them under the project properties-->build-->Conditional compilation symbols. Here you can also see the "debug" and "trace" constants predefined (if checked for your config).

hormberg
  • 184
  • 2
  • 8
0

"Symbols" in this context means identifiers that are called "conditional compilation symbols" and are only useful for the #if directive.

You can set them:

  • In VS from Project properties->build->conditional compilation symbols (as @HimBromBreere wrote) note that there are checkboxes for the commonly used DEBUG and TRACE symbols
  • From the compiler command line with the -define option
  • In the code with the #define directive
Nir
  • 29,306
  • 10
  • 67
  • 103