#define
: This directive is used to define a symbol, which can be used in conditional compilation. For example, #define DEBUG
creates a symbol named DEBUG
.
#undef
: This directive undefines a previously defined symbol. This can be useful if you want to remove a symbol's definition in certain parts of your code.
#if
: This directive checks whether a symbol is defined. If the symbol evaluates to true, the code following this directive is included in the compilation.
csharp
1#if DEBUG
2Console.WriteLine("Debug mode is on.");
3#endif
#else
: This directive provides an alternative block of code that will be included if the condition in the preceding #if
directive is false.
csharp
1#if DEBUG
2Console.WriteLine("Debug mode is on.");
3#else
4Console.WriteLine("Debug mode is off.");
5#endif
#elif
: This directive allows you to check another condition if the previous #if
or #elif
is false. It stands for "else if."
csharp
1#if DEBUG
2Console.WriteLine("Debug mode is on.");
3#elif RELEASE
4Console.WriteLine("Release mode is on.");
5#endif
#endif
: This directive marks the end of a conditional directive started by #if
, #else
, or #elif
.
#line
: This directive allows you to change the line number and optionally the file name that the compiler uses for error and warning messages. This can be useful for generated code.
csharp
1#line 200 "MyFile.cs"
#error
: This directive generates a compilation error with a specific message. It can be used to enforce certain conditions in your code.
csharp
1#error "This code should not be compiled."
#warning
: This directive generates a compiler warning with a specified message. It can be used to alert developers about potential issues.
csharp
1#warning "This code is deprecated."
#region
: This directive allows you to define a block of code that you can collapse or expand in the Visual Studio code editor. It helps organize code visually.
csharp
1#region MyRegion
2// Code here
3#endregion
#endregion
: This directive marks the end of a #region
block.