# #define (C# Reference)

You use #define to define a symbol. When you use the symbol as the expression that's passed to the #if directive, the expression will evaluate to true, as the following example shows:

#define DEBUG


## Remarks

Note

The #define directive cannot be used to declare constant values as is typically done in C and C++. Constants in C# are best defined as static members of a class or struct. If you have several such constants, consider creating a separate "Constants" class to hold them.

Symbols can be used to specify conditions for compilation. You can test for the symbol with either #if or #elif. You can also use the ConditionalAttribute to perform conditional compilation.

You can define a symbol, but you cannot assign a value to a symbol. The #define directive must appear in the file before you use any instructions that aren't also preprocessor directives.

You can also define a symbol with the -define compiler option. You can undefine a symbol with #undef.

A symbol that you define with -define or with #define does not conflict with a variable of the same name. That is, a variable name should not be passed to a preprocessor directive and a symbol can only be evaluated by a preprocessor directive.

The scope of a symbol that was created by using #define is the file in which the symbol was defined.

As the following example shows, you must put #define directives at the top of the file.

#define DEBUG
//#define TRACE
#undef TRACE

using System;

public class TestDefine
{
static void Main()
{
#if (DEBUG)
Console.WriteLine("Debugging is enabled.");
#endif

#if (TRACE)
Console.WriteLine("Tracing is enabled.");
#endif
}
}
// Output:
// Debugging is enabled.


For an example of how to undefine a symbol, see #undef.