Willie Visagie
Willie Visagie

Reputation: 351

How to use the DEBUG constant like a variable

Maybe a duplicate - I just can't figure out what to google...

In C#, is it possible to use the DEBUG constant like a variable such as

Boolean debugging = DEBUG;

I'd like to avoid this:

#if DEBUG
    Boolean debugging = true;
#else
    Boolean debugging = false;
#endif

Thanks in advance!

Upvotes: 1

Views: 3113

Answers (2)

Willie Visagie
Willie Visagie

Reputation: 351

The typical way is to use it like this:

#if DEBUG
   Boolean debugging = true;
#else
   Boolean debugging = false;
#endif

Cheers!

Upvotes: 1

Ruzihm
Ruzihm

Reputation: 20269

Not really. The typical way would be the way you specifically didn't want to do. However, if you're hard set on not using #if, you can use ConditionalAttribute to do something similar. For instance:

public class Program {
    public static void Main(String[] args) {               
        Boolean debug = false;
        CheckForDebug(ref debug);

        Console.WriteLine("debug = " + debug);
    }

    [Conditional("DEBUG")]
    public static void CheckForDebug(ref Boolean debug)
    {
        debug = true;
    }
}

This might be of use to you outside of the specific question you asked here. ConditionalAttribute is useful for making sure a method that returns void is only run when DEBUG (or any arbitrary preprocessor symbol) is defined.

Example: http://rextester.com/JXHE87904

Upvotes: 2

Related Questions