0% found this document useful (0 votes)
20K views

Preprocessor Directives in C#

The document discusses various preprocessor directives in C#, including #if, #else, #elif, #endif, #define, #undef, and #warning. It provides the description and purpose of each directive and includes sample code demonstrating the usage of each directive.

Uploaded by

Niranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20K views

Preprocessor Directives in C#

The document discusses various preprocessor directives in C#, including #if, #else, #elif, #endif, #define, #undef, and #warning. It provides the description and purpose of each directive and includes sample code demonstrating the usage of each directive.

Uploaded by

Niranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Preprocessor Directives in C#

Preprocessor directives are a block of statements that are handled before the actual
compilation process starts. They give some instructions to the compiler on the
compilation process, error or warning handling etc.
Preprocessor directives begin with a hash symbol (#) and do not contain the
semicolon at the end as they are not statements. They are instead terminated by a new
line.
Some of the preprocessor directives in C# are given as follows:

Preprocessor Directive Description

The #if directive compiles the code between the directives only
#if
if the specified symbol is defined.

#else The #else directive allows the user to  create a compound


conditional directive.  If none of the expressions in the
preceding #if or the #elif directives evaluate to true, then
the compiler evaluates all the code between #else and the
subsequent #endif.

#elif The #elif is a compound conditional directive that is evaluated if neither the
preceding #if nor any preceding optional #elif directive expressions evaluate to true.

The #endif directive specifies the end of a conditional directive which began with the
#endif
#if directive.

#define The #define directive is used to define a symbol, which is a sequence of characters.

#undef The #undef directive lets the user to undefine a symbol.

#warning The #warning directive is used to generate a CS1030 level one compiler warning
Preprocessor Directive Description

from a specific location in the code.

The #define Preprocessor Directive

The #define directive is used to define a symbol, which is a sequence of characters.

The symbols that are defined using the #define directive evaluate to true when used

with the #if directive.

A program that demonstrates the #define directive is given as follows:

Source Code: Program that demonstrates the #define directive in C#

#define TEST

using System;

namespace PreprocessorDirectivesDemo

  class Example

  {

     static void Main(string[] args)

     {
        #if (TEST)

           Console.WriteLine("The TEST is defined");

        #else

           Console.WriteLine("The TEST is not defined");

        #endif

     }

  }

The #undef Preprocessor Directive


The #undef directive allows the user to undefine a symbol. The symbols that are
undefined using the #undef directive evaluate to false when used with the #if directive.

A program that demonstrates the #undef directive is given as follows:

Source Code: Program that demonstrates the #undef directive in C#


#undef TEST
using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (TEST)
           Console.WriteLine("The TEST is defined");
        #else
           Console.WriteLine("The TEST is not defined");
        #endif
     }
  }
}

The #if Preprocessor Directive


The #if directive compiles the code between the directives only if the specified symbol is
defined. It is basically used to create a conditional directive. This means that the code
that is inside the #if directive is only compiled if the expression tested with the #if
directive evaluates to true. The #endif directive always the if directive.

A program that demonstrates the #if directive is given as follows:

Source Code: Program that demonstrates the #if directive in C#


#define TEST
using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (TEST)
           Console.WriteLine("The TEST is defined");
        #endif
     }
  }
}

The #elif Preprocessor Directive


The #elif is a compound conditional directive that is evaluated if neither the preceding
#if nor any preceding optional #elif directive expressions evaluate to true. This
preprocessor directive is used when multiple expressions are tested.

A program that demonstrates the #elif directive is given as follows:

Source Code: Program that demonstrates the #elif directive in C#


#undef TEST1
#define TEST2
using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (TEST1 && TEST2)
Console.WriteLine("Both TEST1 and TEST2 are defined");
#elif (TEST1 && !TEST2)
Console.WriteLine("TEST1 is defined and TEST2 is
undefined");
#elif (!TEST1 && TEST2)
Console.WriteLine("TEST1 is undefined and TEST2 is
defined");
#else
Console.WriteLine("Both TEST1 and TEST2 are undefined");
#endif
     }
  }
}

The #warning Preprocessor Directive


The #warning directive is used to generate a CS1030 level one compiler warning from a
specific location in the code.

A program that demonstrates the #warning directive is given as follows:

Source Code: Program that demonstrates the #warning directive in C#


using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (!TEST)
#warning TEST is undefined
#endif
     }
  }
}

You might also like