Using the #warning Preprocessor Directive

Conditional compilation is powerful. One Preprocessor Directive that I like to use to remind myself of a particular condition is #warning. The #warning preprocessor directive will trigger a compilation warning with a specified message when it is hit. For example, you could use it to conditionally compile test code into your application:

#if TEST
#warning Compiled with the TEST flag
  const int SleepFor = 5;
  Console.WriteLine("Sleeping for " + SleepFor + " minutes...");
  System.Threading.Thread.Sleep(SleepFor * 60 * 1000);

Obviously, you wouldn’t want this code to make to a release build. Adding the #warning preprocessor directive helps ensure you see that the conditional code was added to the build.

Hope this helps!

This entry was posted in C#, MSBuild. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *