In this article we will see when and how to use some of the preprocessor directives in C# programming.
Let's take a small break from Design Patterns and learn a small, interesting and somewhat unnoticed concept.
What is a Preprocessor Directive?
Preprocessor directives are commands that are interpreted by the compiler and affect the output or behavior of the build process.
We will see some of them now.
- #region name: Marks the beginning of a region of code; has no compilation effect
- #endregion name: Marks the end of a region of code; has no compilation effect.
- #define identifier: Defines a compilation symbol
- #if expression: If the expression is true, compiles the following section.
- #elif expression: If the expression is true, compiles the following section however the #elif is not evaluated if a previous #if is true
- #else: If the previous #if or #elif expression is false, compiles the following section.
- #endif: Marks the end of an #if construct
- #warning message: Displays a compile-time warning message
- #error message: Displays a compile-time error message
#region and #endregion
Sometimes we come across a situation where we want to give a particular group of code a name.
#region MathsLogic
int number1 = 0;
int number2 = 0;
Console.WriteLine("Enter 2 numbers");
string strNumber1 = Console.ReadLine();
string strNumber2 = Console.ReadLine();
int.TryParse(strNumber1, out number1);
int.TryParse(strNumber2, out number2);
int Sum = number1 + number2;
Console.WriteLine("Your Sum is " + Sum);
#endregion
Use of regions makes code readable, intuitive and easy to manage (Visual Studio, for example, allows you to easily hide or display regions).
#define
- A compilation symbol is an identifier that has only two possible states. It is either defined or undefined.
- The #define directives can be used only at the top of a source file, before any C# code is listed and used to define a compilation symbol.
- The scope of a symbol created using a #define is the file in which it was defined.
#define Test1
#define Test2
using System;
using System.Collections.Generic;
using System.Linq;
#if, #elif, #else, #endif
Sometimes we do write some code which is just needed during development mode, or required only in some conditions.
In the above statement not needed means even not required to compile.
We can do this with the help of these four directives.
#if Test1
Console.WriteLine("Test1 Defined");
#elif Test2
Console.WriteLine("Test1 not Defined but Test2 Defined");
#else
Console.WriteLine("Both Test1 and Test2 are undefined");
#endif
Here Test1 and Test2 are compilation symbols created using #define.
- Output is:
Test1 Defined
- Now place "#define Test1" at the top.
Now the output is:
Test1 not Defined but Test2 Defined
- Now place both "#define Test2" and "#define Test1" at the top.
Now the output is:
Both Test1 and Test2 are undefined
The main difference between working this way and using traditional if…else with global variables is that here the code won't even be compiled if it finds the symbol is not defined.
Download the attached samples for a practical demonstration.
Thanks for reading,
Hope you enjoyed and stay tuned for more such articles. Comments are always welcome.

Gowtham RajamanickamPosted Apr 12, 2016, 2:11 AM
Good One, Thanks for sharing
Sukesh MarlaPosted Sep 11, 2012, 2:30 PM
thanks aman
Aman SinghalPosted Sep 11, 2012, 9:33 AM
good article for understanding preprocessor directives in C#.
Sukesh MarlaPosted Sep 11, 2012, 6:55 AM
You can use them if you want to restrict compliler from compiling something..Like For Example, Consider you want a block has to be complied in debug mode but not it release mode.
Rohatash KumarPosted Sep 11, 2012, 4:07 AM
When to use preprocessor directives in .net ? Sukesh
Sukesh MarlaPosted Sep 10, 2012, 11:07 PM
Thanks Mahesh sir, even i does :)
Sukesh MarlaPosted Sep 10, 2012, 11:06 PM
Thanks a lot for your inputs Akkiraju and Sam I was accepting some inputs in http://www.c-sharpcorner.com/UploadFile/SukeshMarla/Asp-Net-best-practices/ http://www.c-sharpcorner.com/UploadFile/SukeshMarla/Asp-Net-performance-practices/ because always developers always try to do better than previous one, so every input you give will be precious.
Mahesh ChandPosted Sep 10, 2012, 9:28 PM
Good article Sukesh. It is rare to see articles on topic like this. And yes I use #if DEBUG a lot :). Cheers!
Akkiraju IvaturieditedPosted Sep 10, 2012, 7:14 PMEdited Sep 10, 2012, 7:15 PM
I just want to add one more to the above: #if DEBUG //code to run when in debug mode #else //code to run when in release mode #endif
Sam HobbsPosted Sep 10, 2012, 5:56 PM
Thank you Sukesh for the somewhat unnoticed concepts. I just want to add that in case anyone is wondering why #define is not used more, the answer is that many experienced developers consider it to be something to be avoided and obviously Microsoft agrees. The #define in C# is very much like #define in C++ and in C++ a #define is also called a macro. See what the designer of C++, Bjarne Stroustrup, says about macros in: http://www.stroustrup.com/bs_faq2.html#macro