Using the DEBUG Conditional Compilation Symbol

By default, Visual Studio creates a Debug build configuration in which the Define DEBUG constant option is checked.
You can check to see if this constant (or “conditional compilation symbol”) is defined in your code, using the#if directive.
1
2
3
4
5
6
7
8
9
10
11
12
        public void Bark()
        {
#if DEBUG
            Console.WriteLine("DEBUG INFO: Entering Dog.Bark() [{0}]", Name);
#endif
 
            Console.WriteLine("{0} says woof", Name);
 
#if DEBUG
            Console.WriteLine("DEBUG INFO: Exiting Dog.Bark() [{0}]", Name);
#endif
        }
Code between the #if DEBUG and matching #endif will be compiled only if the DEBUG compilation symbol has been defined.  By default, this will be true for the Debug build configuration, but not the Release build configuration.
When we select the Debug build configuration and re-build, we see the following output:
When we select the Release build configuration, where the DEBUG symbol is no longer defined, we see the following output: