Deﬁning and Undeﬁning Symbols C# with Example
A compiler symbol is a keyword that is deﬁned at compile-time that can be checked for to conditionally execute
speciﬁc sections of code.
There are three ways to deﬁne a compiler symbol. They can be deﬁned via code:
They can be deﬁned in Visual Studio, under Project Properties > Build > Conditional Compilation Symbols:
(Note that DEBUG and TRACE have their own checkboxes and do not need to be speciﬁed explicitly.)
Or they can be deﬁned at compile-time using the /define:[name] switch on the C# compiler, csc.exe.
You can also undeﬁned symbols using the #undefine directive.
The most prevalent example of this is the DEBUG symbol, which gets deﬁned by Visual Studio when an application is
compiled in Debug mode (versus Release mode).
public void DoBusinessLogic()
catch (Exception ex)
In the example above, when an error occurs in the business logic of the application, if the application is compiled in
Debug mode (and the DEBUG symbol is set), the error will be written to the trace log, and the exception will be re-
thrown for debugging. However, if the application is compiled in Release mode (and no DEBUG symbol is set), a
logging framework is used to quietly log the error, and a friendly error message is displayed to the end user.