#117 – Use #define to Define a Symbol

In addition to defining symbols by specifying them in the project properties dialog (“conditional compilation symbols”), you can define symbols directly in your code using the #define directive.  This is helpful when you want to have a conditional compilation symbol appear in some files, but not throughout the entire project.

Here’s an example.  Note that the #define must be the first thing in the file.

#define LOGGING

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

#if LOGGING
            DoSomeLogging();
#endif
            uint x = 0x1234;
        }
    }
}
Advertisements