C# preprocessor instruction


Release date:2023-09-01 Update date:2023-10-13 Editor:admin View counts:411

Label:

C# preprocessor instruction

The preprocessor instruction instructs the compiler to preprocess the information before the actual compilation begins.

All preprocessor instructions start with #. And on a line, only white space characters can appear before preprocessor instructions. Preprocessor instructions are not statements, so they do not end with a semicolon (;).

The C # compiler does not have a separate preprocessor, but instructions areprocessed as if they were a separate preprocessor. In C #, preprocessorinstructions are used to play a role in conditional compilation. Unlike C and C++, they are not used to create macros. A preprocessor instruction mustbe the only instruction on the line.

C# preprocessor instruction list

The following table lists the preprocessor instructions available in C#:

Preprocessor instruction

Description

#define

It is used to define a series of characters that become symbols.

#undef

It is used to undefine symbols.

#if

It is used to test whether the symbol is true.

#else

It is used to create compound conditional instructions to be used with # if.

#elif

It is used to create compound conditional instructions.

#endif

Specifies the end of a conditional instruction.

#line

It allows you to modify the number of lines of the compiler and (optionally)the file name that outputs errors and warnings.

#error

It allows an error to be generated from a specified location in the code.

#warning

It allows first-level warnings to be generated from a specified location inthe code.

#region

It allows you to specify a block of code that can be expanded or collapsedwhen using the outline feature of Visual Studio Code Editor.

#endregion

It marks the end of the #region block.

#define preprocessor

#define preprocessor instructions create symbolic constants.

#define allows you to define a symbol so that it can be passed to the #if instruction, which will return true . Its syntax is as follows:

#define symbol

The following procedure illustrates this:

Example

#define PI
using System;
namespace PreprocessorDAppl
{
   class Program
   {
      static void Main(string[] args)
      {
         #if (PI)
            Console.WriteLine("PI is defined");
         #else
            Console.WriteLine("PI is not defined");
         #endif
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

PI is defined

Conditional instruction

You can use the #if directive to create a conditional instruction. Conditional instructions are used to test whether the symbol is true. If true, the compiler executes #if code between and the nextinstruction.

Syntax for conditional instructions:

#if symbol [operator symbol]...

Among them symbol is the name of the symbol to test. You can also use the true and false, or place a negative operator before the symbol

Common operators are:

  • == (equal to)

  • != (not equal to)

  • && (with)

  • \|\| (or)

You can also group symbols and operators in parentheses. Conditional directives are used to compile code when debugging a version or when compiling a specified configuration. One with #if instruction begins with a conditional instruction that must be displayed with a #endif command terminated.

The following program demonstrates the use of conditional instructions:

Example

#define DEBUG
#define VC_V10
using System;
public class TestClass
{
   public static void Main()
   {
      #if (DEBUG && !VC_V10)
         Console.WriteLine("DEBUG is defined");
      #elif (!DEBUG && VC_V10)
         Console.WriteLine("VC_V10 is defined");
      #elif (DEBUG && VC_V10)
         Console.WriteLine("DEBUG and VC_V10 are defined");
      #else
         Console.WriteLine("DEBUG and VC_V10 are not defined");
      #endif
      Console.ReadKey();
   }
}

When the above code is compiled and executed, it produces the following results:

DEBUG and VC_V10 are defined

Powered by TorCMS (https://github.com/bukun/TorCMS).