C# Define and Undef Directives

Define preprocessor directive

#define and #undef directives influence compilation. They should appear at the very top of a source text file. They can adjust compilation options for the entire file. As directives, they have no affect on runtime performance.

This C# tutorial shows the use of the #define and #undef preprocessor directives.

Example

Note

We introduce the #define and #undef directives at the very top of the file. The text defines the symbol B, then defines the symbol A, then undefines the symbol B. Therefore, the program will compile with A being defined, and B being undefined. The undef directive is the exact opposite of the define directive, and it doesn't matter if the symbol was never defined in the first place.

Program that uses #define and #undef directives [C#]

// Define B, then define A.
// ... You can use undef at the top here too.
// ... Try changing A to C.
#define B
#define A
#undef B
using System;

class Program
{
    static void Main()
    {
	// Use an if/elif/endif construct.
#if A
	Console.WriteLine("a");
#elif B
	Console.WriteLine("b");
#elif C
	Console.WriteLine("c");
#endif
	// Use an if/else/endif construct.
#if A
	Console.WriteLine("a2");
#else
	Console.WriteLine("!a2");
#endif
    }
}

Output

a
a2
Pound (#) symbol

About #define. In the C# language, the #define line is considered a preprocessing directive. There are some invalid syntaxes for defined symbols; you cannot use a number value as the defined identifier, for example. Instead of reading the language specification, you can just experiment in the compiler to see what works.

About #undef. The #undef directive ensures that after the textual point in the file, the specified identifier is not defined. Therefore, in the program text, the #undef B cancels out the #define B directive, leaving B not defined.

About #if. The #if directive is evaluated as preprocessing time, not runtime. It works in a similar way as the if-statement in the C# language itself. In this program, we use the #if directive in two places. When the A identifier is defined, the #if A directives will result in the following statements being compiled into the metadata.

About #elif. The #elif directive is exactly the same as the #if directive, except it must follow an #if or other #elif directive itself. It is similar to the "else if" construct in the C# language. It is evaluated in textual order at preprocessing time.

About #endif. The #endif directive serves to terminate the #if...#elif... or #if...#else directive structures. You must use it to signal that the preprocessor should stop ignoring code in a statement list that is being preprocessed out.

About #else. The #else directive can come after a #if or #elif directive statement. It provides the default case for a conditional directive construct in the C# language. You must follow the #else directive with a #endif directive.

Directives

Using keyword

The term "directive" indicates a line in the program text that is not part of the program's execution logic. In other words, directives provide hints about how the program is compiled, not what it does with instructions and the evaluation stack. In the C# language, the "using System;" line is also a directive, but not a preprocessing directive.

Using System

Summary

The C# programming language

We demonstrated the #define and #undef preprocessing directives in the C# language. These directives provide a way to conditionally compile or remove parts of the source text. Sometimes, these directives can be a clue that a file is overly complex or not well-organized. Please see section 9.5 of the ECMA-344 "C# Language Specification", "Pre-processing directives" for a complete reference.

Directives
.NET