#490 – 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.

        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:

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #490 – Using the DEBUG Conditional Compilation Symbol

  1. Pingback: #491 – Use Debug.WriteLine to Output Debug Information « 2,000 Things You Should Know About C#

  2. Pingback: #491 – Use Debug.WriteLine to Output Debug Information « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: