C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: If both of these are defined, we want to force an error so that the program won't compile.
Tip: This approach to compiler errors can eliminate problematic combinations of settings.
C# program that uses error directive
#define A
#define B
#if A && B
#error Never define A and B at the same time!
#endif
class Program
{
static void Main()
{
}
}
Tip: Another related directive you can employ is the #warning directive. This does not force a fatal error in compilation.
Instead: It just causes a nagging warning. Depending on the severity of the compile-time problem, consider the #warning directive.
Info: You can use the #warning directive by placing the #warning text followed by the text warning, terminated by a newline.
Tip: You do not need quotation marks surrounding the text. When this program is compiled, you can see the Error List.
C# program that uses warning directive
using System.Diagnostics;
#warning Don't run this program.
class Program
{
static void Main()
{
#warning Get back to work.
Process.Start("http://www.reddit.com/");
}
}
Tip: You can see further examples of #warning and #error in section 2.5.5 of the C# specification annotated third edition.
Note: The #warning directive is different from an Assert call. It is triggered at compile-time, not at run-time.
Debug.WriteNote 2: You cannot warn with this directive on the run-time aspects of your program. The directive is processed too early.