Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is #if DEBUG and How to use it in C#?
In Visual Studio, Debug mode and Release mode are different configurations for building your .NET project. The #if DEBUG directive is a preprocessor conditional compilation directive that allows you to include or exclude code blocks based on whether the DEBUG symbol is defined.
The Debug mode does not optimize the binary it produces because the relationship between source code and generated instructions is more complex. This allows breakpoints to be set accurately and allows programmers to step through the code one line at a time. The Debug configuration compiles with full symbolic debug information, while the Release configuration has no symbolic debug information and is fully optimized.
Syntax
Following is the syntax for using #if DEBUG conditional compilation −
#if DEBUG // Code executed only in Debug mode #endif
You can also use #else for alternative code execution −
#if DEBUG // Debug mode code #else // Release mode code #endif
How It Works
The code written inside #if DEBUG will be executed only when the DEBUG symbol is defined, which happens automatically in Debug configuration. If the code is running in Release mode, then #if DEBUG evaluates to false and the code inside this block is not compiled into the final assembly.
Changing Build Configuration
You can change the build configuration in two ways −
-
From the Build menu, select Configuration Manager, then select Debug or Release.
-
On the toolbar, choose either Debug or Release from the Solution Configurations list.
Using #if DEBUG for Debug-Only Code
Example
using System;
class Program {
static void Main() {
Console.WriteLine("Application started");
#if DEBUG
Console.WriteLine("You are in Debug mode");
Console.WriteLine("Additional debug information enabled");
#endif
Console.WriteLine("Application running...");
}
}
When running in Debug mode, the output is −
Application started You are in Debug mode Additional debug information enabled Application running...
When running in Release mode, the output is −
Application started Application running...
Using #if DEBUG with #else
Example
using System;
class LoggingExample {
static void Main() {
string message = "Processing data...";
#if DEBUG
Console.WriteLine("[DEBUG] " + message);
Console.WriteLine("[DEBUG] Timestamp: " + DateTime.Now);
#else
Console.WriteLine("[INFO] " + message);
#endif
Console.WriteLine("Task completed");
}
}
In Debug mode, the output is −
[DEBUG] Processing data... [DEBUG] Timestamp: 12/10/2023 2:30:15 PM Task completed
Common Use Cases
-
Debug logging − Adding detailed logging that should not appear in production builds.
-
Performance testing − Including timing code or memory usage checks only during development.
-
Test data − Using mock data or test configurations in Debug mode.
-
Assertions − Adding validation checks that are removed from optimized Release builds.
Example with Debug Assertions
using System;
class ValidationExample {
static void Main() {
int[] numbers = {1, 2, 3, 4, 5};
int index = 2;
#if DEBUG
if (index < 0 || index >= numbers.Length) {
Console.WriteLine("DEBUG: Index out of bounds!");
return;
}
Console.WriteLine("DEBUG: Validation passed");
#endif
Console.WriteLine("Value at index " + index + ": " + numbers[index]);
}
}
The output in Debug mode is −
DEBUG: Validation passed Value at index 2: 3
Conclusion
The #if DEBUG directive enables conditional compilation in C#, allowing you to include debug-specific code that gets completely removed from Release builds. This is essential for adding logging, validation, and testing code that should not impact production performance or expose sensitive information.
