Preprocessor Directives in C#

In C#, we can submit more information to the compiler by using preprocessor directives. There are different types of pre-processor directives available in C#, the most common used directives are conditional directives. For example, we can use if directive on DEBUG mode as shown below. Open Microsoft Visual Studio 2015 and create a new console application and name it as CSharpPreprocessorDirectives, add the below code. 

using System;

namespace CSharpPreprocessorDirectives

{

    class Program

    {

        static void Main(string[] args)

        {

#if DEBUG

            Console.WriteLine("This is DEBUG directive");

#endif

            Console.ReadLine();

        }

    }

} 

It displays output as “This is DEBUG directive” when you run the application in debug mode and if you run in Release mode, it will not display anything because we have #if directive defined which executes only in debug mode. We can set our symbols using #define directives in C#. Let’s create a directive symbol TESTING. First, we have to set the directive. 

#define TESTING

using System;

namespace CSharpPreprocessorDirectives

{

    class Program

    {

        static void Main(string[] args)

        {

#if TESTING

            Console.WriteLine("This is TESTING directive");

#endif

            Console.ReadLine();

        }

    }

} 

As shown, at the start of the code we define the TESTING using define pre-processor directive and we are checking whether it defined or not by using #if directives. We can add multiple conditions for a #if directive by using &&, ||, and ! for symbols as shown below. 

#if TESTING && DEBUG

            Console.WriteLine("This is TESTING directive");

#endif 

The above code executes only if TESTING defined and also code executes in debug mode only. There are several preprocessor directives available in C#, and those explained below. 

#define: It used to define the symbol.

#undef: It used to undefine the symbol.

#if: It used to validate the conditions. We can use ==, !=, ||, and && operators to validate the conditions for #if directive.

#else: It executes the code subsequent to #if.

#elseif: It used to validate one more condition after #if and before #else directives.

#warning text: It displays the warning with text at compiler output.

#error text: It displays the error text at compiler output

#pragma warning [restore | disable]: Restores or disables the compiler warnings

#line [ number ["file"] | hidden]: number specifies the line in source code, file is the filename to appear in computer output, and hidden instructs debuggers to skip over code from this point until the next #line directive.

#region name: It used to mark the beginning of region with the name.

#endregion: It ends an outline region. 

In C#, the compiler generates the warnings when it finds something in the code that seems unintentional. Warnings are precious to find bugs in the code, and at the same time, false warnings may create the problems. Sometimes we may require disabling all warnings which we already know or all warnings across the application. By using #pragma directive, we can disable the warning for a particular field. 

Open Microsoft Visual Studio 2015 => Create a console application and add some static field as shown below. 

using System;

namespace CSharpPreprocessorDirectives

{

    class Program

    {

        static int iId = 100; 

        static void Main(string[] args)

        {

        }

    }

} 

Run the application; we get a warning message as shown below.

We can disable this warning message by using #pragma directive as shown below.

using System;

namespace CSharpPreprocessorDirectives

{

    class Program

    {

#pragma warning disable 414

        static int iId = 100;

#pragma warning restore 414 

        static void Main(string[] args)

        {

        }

    }

}

As shown above, we disable the warning for field iId and re-enables it after; now we do not get any warnings for field iId. If you want to disable for the entire class, just declare #pragma warning disable directive at the top of the class as shown below. 

#pragma warning disable 414

using System;

namespace CSharpPreprocessorDirectives

{

    class Program

    {

        static int iId = 100;

        static string sName = "Raj"; 

        static void Main(string[] args)

        {

        }

    }

} 

If you want to treat warnings as errors for your application, go to project properties and select “Treat warnings as errors” All radio button under Build tab as shown below.