
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is a conditional pre-processor directive in C#?
Use the #if directive to create a conditional directive. Conditional directives are useful for testing a symbol or symbols to check if they evaluate to true. If they do evaluate to true, the compiler evaluates all the code between the #if and the next directive.
Here is the syntax −
#if symbol [operator symbol]...
Here, symbol is the name of the symbol you want to test. You can also use true and false or prepends the symbol with the negation operator.
The operator symbol is the operator used for evaluating the symbol. Operators could be either of the following −
- == (equality)
- != (inequality)
- && (and)
- || (or)
Here is an example showing the usage of conditional pre-processor directives in C# −
Example
#define DEBUG #define VC_V10 using System; public class Demo { public static void Main() { #if (DEBUG && !VC_V10) Console.WriteLine("DEBUG is defined"); #elif (!DEBUG && VC_V10) Console.WriteLine("VC_V10 is defined"); #elif (DEBUG && VC_V10) Console.WriteLine("DEBUG and VC_V10 are defined"); #else Console.WriteLine("DEBUG and VC_V10 are not defined"); #endif Console.ReadKey(); } }
Output
DEBUG and VC_V10 are defined
- Related Articles
- What is a pre-processor directive in C#?
- What is #define pre-processor directive in C#?
- What are C# pre-processor directives?
- What are the Pre-processor Commands in C language?
- Explain the pre-processor directives in C language
- What is a Ternary operator/conditional operator in C#?
- What is conditional compilation in C language?
- What is Superscalar Processor?
- What is CISC Processor?
- What is RISC Processor?
- What is a page directive in JSP?
- What is a include directive in JSP?
- What is a taglib directive in JSP?
- What is a @extend directive in SASS?
- What are conditional attributes in C#?

Advertisements