Error and warning preprocessor directives

Generate user-defined compiler error and warnings with preprocessor directives

Home DailyDrop

Daily Knowledge Drop

There are a number of C# directives which can be used to instruct the compiler to generate user-defined errors and warnings as well as control line information:

  • #error
  • #warning
  • #line

#error

The #error directive can be used to generate a compiler error with a specific message.

Suppose (for no good reason I can think of), the below code should only be executed when in Release build configuration:

void MethodToOnlyRunInRelease()
{
#if DEBUG
#error Method MethodToOnlyRunInRelease should only be run in release mode
#endif

    // Execute production only logic
}

The #error directive is used to raise an error with the compiler when the code is built in DEBUG configuration.

In Visual Studio, this will reflect in the Error List (as well as in the build output) as follows:

#error directive

#warning

The #warning directive can be used to generate a compiler warning with a specific message.

Suppose while coding you notice a method is using a deprecated portion of code. A warning note can be left to have it investigated further in future:

void MethodToDoSomething()
{
#warning this method looks to be using deprecated code. Needs investigating

    // Code which does something
}

This will raise a warning on the specified line, which will reflect in Visual Studio, in the Error List as a warning (as well as in the build output) as follows:

#warning directive

#line

The #line directive can be used to change the line number printed with the compiler message

#line 1201 
void AnotherMethodNeverCalled()
{

}

In Visual Studio, this will reflect in the Error List as a warning (as well as in the build output) as follows, notice the line number column value.

#line directive

Instead of the actual file line number, the error will appear on the line relative to the specified line number. In this case, the warning appears on the first line after #line 1201, hence it is shown as line 1201.

When using the #line directive, the filename can also be overwritten:

#line 1 "High performance methods"
void MethodNeverCalled()
{

}

In Visual Studio, when a warning or error originates from this area of code, it will reflect in the Error List (as well as in the build output) as follows, notice the line number column value and the File column value:

#line and filename directive


Daily Drop 69: 09-05-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net directives visualstudio