C# FAQ: What is the define preprocessor directive
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:FAQs |
| edit |
What is the #define preprocessor directive?
As a member of the C family of programming languages, C# supports preprocessor directives—e.g., #define, #if, and #endif. (Technically speaking, csc.exe does not have a preprocessor; because, these symbols are resolved during the compiler's lexical analysis phase.)
The #define directive enables the creation of custom symbols for controlling code compilation. Unlike C++, the #define directive of C# does not allow you to create macro-style code. Once a symbol has been defined, the #if and #endif directives may be employed to test for that symbol.
The following code represents a typical usage for #define:
#define DEBUG using System; public class TestClass { public static void Main() { // conditional compilation based on the DEBUG symbol #if DEBUG Console.WriteLine ("The DEBUG symbol has been defined!"); #endif } }
When using the #define directive, the symbol is only available within the source file in which it has been defined. However, to define project-wide symbols, simply access the project's property page, navigate to "Configuration Properties | Build", and edit the "Conditional Compilation Constants" field.
In order to disable a constant for a specific file, use the #undef directive.
Also, the C# preprocessor directives are available from ASP.NET as well.
See also
- Does C# have macros or a preprocessor?
- Is there C# support for C-type macros?
- What is the
definepreprocessor directive?