C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, preprocessor directives are instructions that control the compilation process and help you manage your code in various ways. Preprocessor directives are not statements and do not end with a semicolon. They begin with a #
symbol and are processed by the C# compiler before the actual compilation of your code begins.
In this tutorial, we will cover some common preprocessor directives:
Let's begin!
#define
is used to define a symbol that can be used as a conditional compilation constant. #undef
is used to undefine a symbol.
Example:
#define DEBUG
These directives allow you to conditionally compile sections of your code based on whether a symbol is defined or not. They can be used in combination with #define
and #undef
to enable or disable code sections based on certain conditions.
Example:
#define DEBUG public class MyClass { public void MyMethod() { #if DEBUG Console.WriteLine("Debug mode is enabled."); #else Console.WriteLine("Debug mode is disabled."); #endif } }
In this example, if the DEBUG
symbol is defined, the "Debug mode is enabled." message will be printed. Otherwise, the "Debug mode is disabled." message will be printed.
#warning
is used to generate a warning message during the compilation process, while #error
is used to generate an error message that will stop the compilation process.
Example:
#warning This is a warning message. #error This is an error message.
These directives are used to define collapsible regions in your code. They can help you organize your code, making it easier to navigate and maintain.
Example:
public class MyClass { #region Properties public string Name { get; set; } public int Age { get; set; } #endregion #region Methods public void SayHello() { Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old."); } #endregion }
In this example, the Properties
and Methods
regions are defined, which can be collapsed and expanded in your code editor for better code readability.
That's it! You've now learned the basics of preprocessor directives in C#. Preprocessor directives can help you manage your code, control the compilation process, and conditionally compile sections of your code based on certain conditions. Keep in mind that overusing preprocessor directives can make your code more difficult to read and maintain, so use them judiciously.
#define and #undef in C#
Use #define
to create a symbol and #undef
to undefine it.
#define DEBUG
#if, #else, and #endif in C#
Conditionally include or exclude code blocks based on predefined symbols.
#if DEBUG Console.WriteLine("Debug mode"); #else Console.WriteLine("Release mode"); #endif
#region and #endregion in C# preprocessor directives
Use #region
and #endregion
to create collapsible regions in the code editor.
#region MyRegion // Your code here #endregion
#warning and #error in C# preprocessor
Generate warning or error messages during compilation.
#warning This is a warning message #error This is an error message
Using #pragma in C# preprocessor directives
#pragma
provides additional instructions to the compiler.
#pragma warning disable 0168 // Code that generates a warning (CS0168) is ignored #pragma warning restore 0168
Preprocessor directives for platform-specific code in C#
Conditionally compile code based on the target platform.
#if NETCOREAPP3_1 // Code specific to .NET Core 3.1 #endif
Conditional compilation symbols in C#
Symbols can be defined in project properties or using #define
directives.
#define DEBUG
Custom preprocessor symbols in C#
Define custom symbols to conditionally include code.
#define MY_SYMBOL #if MY_SYMBOL // Code for MY_SYMBOL #endif