Array Collections File String Windows VB.NET Algorithm ASP.NET Cast Class Compression Convert Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method .NET Number Regex Sort StringBuilder Struct Switch Time Value

Compilers operate in phases. In an early phase, your C# program is read and statements called preprocessor directives are handled. You can add or remove blocks of code using symbols with these directives. A common symbol is #define. We explain how the C# compiler deals with directives.
A preprocessing directive always occupies a separate line of source code and always begins with a # character and a preprocessing directive name. Hejlsberg et al., p. 75
This program shows how you can define a symbol in your program with a #define directive. This needs to be at the top of your source code file. Then, you can use #if directives to detect whether that #define was specified. The program shows that #if PERL will evaluate to true, while #if PYTHON will not.
Program that uses preprocessor directives [C#]
#define PERL
using System;
class Program
{
static void Main()
{
#if PERL
Console.WriteLine(true);
#endif
#if PYTHON
Console.WriteLine(false);
#endif
}
}
Output
TrueThese are 12 preprocessor directives in the C# language. Each requires a separate line in your code code and begins with the # character. We initially list them alphabetically by their names, and then describe them in greater detail.
#define
#elif
#else
#endif
#endregion
#error
#if
#line
#pragma
#region
#undef
#warningOverview: These C# examples show how to use preprocessor directives. They cover #define, #if and #region.
Note: Many of these links point to articles that cover multiple preprocessor directives together.
#define and #undef. The most popular directives in C-style languages such as C# are the define directives. These allow you to set symbols at the top of a program file and affect what lines are included in the program. This article provides examples for #define and #undef in the language.
Define and Undef Directives
#if, #elif, and #endif. To use the defined symbols in your program to conditionally compile parts, you need to use the #if, #elif and #endif directives. This article illustrates how you can use these directives with defined symbols.
If Preprocessing Directive: Elif and Endif#region. Another useful directive in the C# language is the #region directive. This gives you a way to separate blocks of code in the integrated development environment, as demonstrated in this example.
Region#warning and #error. You can issue compile-time warnings in the source code of your C# programs with the #warning directive. This directive can be nested inside #if directives for more complex behaviors. The #error directive works in the same way but causes a fatal error.
Warning Directive Error Directive#line. The #line directive is not one of the most frequently useful ones. Its use is mainly limited to situations where you are generating or rewriting C# code with another tool, which is referred to as meta-programming.
Line Directive
#pragma. Does your program generate a warning every time it is compiled, but you expect this warning and don't want to fix it? If so, you can hide the warning by using a #pragma directive. Here are some examples.
Pragma Directive Unreachable Code Detected
Directives, along with comments, do not affect your C# programs during runtime. Unfortunately, by changing how your program text is compiled, directives can lead to various issues such as bit rot that can make programming very frustrating—more so than usual.
Bit Rot